فهرست منبع

0716 Java基础回顾

Qing 10 ماه پیش
والد
کامیت
4248401e9f

+ 46 - 0
springboot-demo/src/main/java/com/sf/javase/BaseMain.java

@@ -0,0 +1,46 @@
+package com.sf.javase;
+
+import java.util.Scanner;
+
+public class BaseMain {
+
+    public static void main(String[] args) {
+//        double d1 = 0.01;
+//        for (int i = 0; i < 100; i++) {
+//            d1 += 0.01;
+//        }
+//        System.out.println(d1);
+//
+
+        // 短路与 如果A && B 当A=false 那么结果为false 此时B不处理
+        // 短路或 A || B  当A=true 那么结果为true
+//        if (test2() && test1()) {
+//            System.out.println("ok");
+//        } else {
+//            System.out.println("不ok");
+//        }
+
+
+        // 计算绝对值
+        // 使用程序计数器 记录代码执行的位置
+        Scanner scanner = new Scanner(System.in);
+        int num = scanner.nextInt();
+        if (num > 0) {
+            System.out.println(num);
+        } else {
+            System.out.println(-num);
+        }
+    }
+
+
+    public static boolean test1() {
+        System.out.println("test1");
+        return true;
+    }
+
+    public static boolean test2() {
+        System.out.println("test2");
+        return false;
+    }
+
+}

+ 103 - 0
springboot-demo/src/main/java/com/sf/javase/LinkedListMain.java

@@ -0,0 +1,103 @@
+package com.sf.javase;
+
+import java.util.ArrayList;
+import java.util.LinkedList;
+
+public class LinkedListMain<E> {
+
+    public static void main(String[] args) {
+        ArrayList<String> list1 = new ArrayList<>();
+        list1.add("a");
+
+        LinkedList<String> list = new LinkedList<>();
+        list.add("a");
+        // 在指定位置插入元素
+        // 根据位置找到元素  再插入
+        list.add(0, "b");
+
+        LinkedListMain<String> myList = new LinkedListMain<>();
+        myList.add("a");
+        myList.add("b");
+        System.out.println(myList);
+    }
+
+    public boolean add(E e) {
+        // 在链表尾部插入元素
+        Node<E> l = last;
+        // 将要添加的元素 创建一个节点
+        Node<E> newNode = new Node<>(l, e, null);
+        // 将尾节点更新为新的节点
+        last = newNode;
+        // 验证是否是第一次添加元素
+        if (l == null) {
+            // 此时添加的元素也是头节点
+            head = newNode;
+        } else {
+            // 如果之前的尾结点不为空
+            l.next = newNode;
+        }
+        size++;
+        return true;
+    }
+
+    public void add(int index, E e) {
+        // 判断是否越界
+        // 判断是否是最后一个位置
+        if (index == size) {
+            add(e);
+            return;
+        }
+        // 找到当前元素
+        Node<E> curr = node(index);
+        // 可以放在当前元素之前 也可以放在之后 linkedlist源代码是放在之前
+        Node<E> prev = curr.prev;
+        // 构造节点 和前置后置的关系
+        Node<E> newNode = new Node<>(prev, e, curr);
+        curr.prev = newNode;
+        if (prev != null) prev.next = newNode;
+        // 如果prev为空 代表curr是头节点 插入新元素后 头节点变化
+        else head = newNode;
+        size++;
+    }
+
+    // 通过指定位置获取元素
+    public Node<E> node(int index) {
+        // 索引为0 对应头节点
+        // 优化 根据索引的位置 决定从前往后遍历 还是从后往前遍历
+        // 位运算  10<<100 乘2  100>>10 除2
+        if (index < (size >> 1)) {
+            // 从头节点往后遍历
+            Node<E> x = head;
+            for (int i = 0; i < index; i++) {
+                x = x.next;
+            }
+            return x;
+        } else {
+            // 从后往前遍历
+            Node<E> x = last;
+            for (int i = size - 1; i > index; i--) {
+                x = x.prev;
+            }
+            return x;
+        }
+    }
+
+    // 头节点和尾节点
+    private Node<E> head;
+    private Node<E> last;
+    // 当前链表的长度
+    private int size;
+
+    // 双向链表节点的基本结构
+    private static class Node<E> {
+        E item;
+        Node<E> next;
+        Node<E> prev;
+
+        Node(Node<E> prev, E element, Node<E> next) {
+            this.item = element;
+            this.next = next;
+            this.prev = prev;
+        }
+    }
+}

+ 24 - 0
springboot-demo/src/main/java/com/sf/javase/ListMain.java

@@ -0,0 +1,24 @@
+package com.sf.javase;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+public class ListMain {
+
+    public static void main(String[] args) {
+        List<String> list = new ArrayList<>();
+        list.add("a");
+        list.add("b");
+        list.add("c");
+
+        // 洗牌
+        Collections.shuffle(list);
+        System.out.println(list);
+
+        List list1 = new ArrayList();
+        list1.add("a");
+        list1.add(1);
+        list1.add(new int[]{1});
+    }
+}

+ 21 - 0
springboot-demo/src/main/java/com/sf/javase/MethodMain.java

@@ -0,0 +1,21 @@
+package com.sf.javase;
+
+public class MethodMain {
+
+    public static void main(String[] args) {
+        // 0100
+        int c = 123;
+        int d = 456;
+        // 实参
+        int e = myFunc(c,d);
+        System.out.println(e);
+    }
+
+    // 0260
+    private static int myFunc(int a, int b) {
+        // 形参
+        // 函数的调用,有一个将实参赋值给形参的过程
+        // a = 123 , b = 456
+        return a + b;
+    }
+}

+ 30 - 0
springboot-demo/src/main/java/com/sf/javase/thread/MyCallable.java

@@ -0,0 +1,30 @@
+package com.sf.javase.thread;
+
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.FutureTask;
+
+// callable的泛型 就是call方法的返回类型
+// Callable 和 Runnable的区别
+// call()方法相比run()方法,有返回值,还可以抛出异常
+// 泛型对应返回类型 泛型和FutureTask也相一致
+// FutureTask本质上是Runnable的实现类 所以可以将其作为构造参数传给Thread
+public class MyCallable implements Callable<Integer> {
+
+    @Override
+    public Integer call() throws Exception {
+        System.out.println("call begin");
+        return 0;
+    }
+
+    public static void main(String[] args) throws ExecutionException, InterruptedException {
+        MyCallable myCallable = new MyCallable();
+        // 要把callable作为futureTask的参数
+        FutureTask<Integer> futureTask = new FutureTask<>(myCallable);
+        // 创建线程并执行
+        new Thread(futureTask).start();
+
+        Integer result = futureTask.get();
+        System.out.println(result);
+    }
+}

+ 31 - 0
springboot-demo/src/main/java/com/sf/javase/thread/MyThread1.java

@@ -0,0 +1,31 @@
+package com.sf.javase.thread;
+
+public class MyThread1 extends Thread {
+
+    int i;
+
+    // 继承是用子类重写了父类的run方法  就是实际的执行逻辑
+    @Override
+    public void run() {
+        // 获取线程名字
+        System.out.println(getName() + " begin");
+        for (; i < 10; i++) {
+            System.out.println(Thread.currentThread().getName() + ": " + i);
+        }
+    }
+
+    public static void main(String[] args) {
+        for (int i = 0; i < 30; i++) {
+            System.out.println(Thread.currentThread().getName() + ": " + i);
+            if (i == 10) {
+                Thread t1 = new MyThread1();
+                t1.start();
+                // 在获取资源后 真正执行的是run方法
+            }
+            if (i == 20) {
+                Thread t2 = new MyThread1();
+                t2.start();
+            }
+        }
+    }
+}

+ 29 - 0
springboot-demo/src/main/java/com/sf/javase/thread/MyThread2.java

@@ -0,0 +1,29 @@
+package com.sf.javase.thread;
+
+public class MyThread2 implements Runnable {
+
+    int i;
+
+    // 实现的方式 是将Thread中的Runnable类型属性 target进行赋值
+    // 当Thread对象执行run方法时,会先判断 target是否存在,如果存在就调用target的run方法
+    @Override
+    public void run() {
+        for (; i < 15; i++) {
+            System.out.println(Thread.currentThread().getName() + ": " + i);
+        }
+    }
+
+    public static void main(String[] args) {
+        for (int i = 0; i < 30; i++) {
+            System.out.println(Thread.currentThread().getName() + ": " + i);
+            if (i == 5) {
+                Runnable myThread2 = new MyThread2();
+                Thread t1 = new Thread(myThread2);
+                Thread t2 = new Thread(myThread2);
+                t1.start();
+                t2.start();
+            }
+        }
+
+    }
+}

+ 31 - 0
springboot-demo/src/main/java/com/sf/javase/thread/MyThread3.java

@@ -0,0 +1,31 @@
+package com.sf.javase.thread;
+
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.FutureTask;
+
+public class MyThread3 implements Callable<String> {
+    // 此时是call方法
+    @Override
+    public String call() throws Exception {
+        Thread.sleep(2000);
+        return "hello callable";
+    }
+
+    public static void main(String[] args) throws ExecutionException, InterruptedException {
+        // Callable Future
+        Callable<String> callable = new MyThread3();
+        // 用来接收callable执行的结果
+        FutureTask<String> futureTask = new FutureTask<>(callable);
+        // 相当于把FutureTask作为Runnable传给Thread
+        // 所以Thread执行run()方法时 执行的是FutureTask的run()方法
+        // 而FutureTask的run()方法 又调用了Callable的call()方法
+        // 所以最终效果是 call()才是真正的线程执行逻辑
+        Thread thread = new Thread(futureTask);
+        thread.start();
+
+        // 通过get 接收结果 是一种阻塞的等待
+        String result = futureTask.get();
+        System.out.println(result);
+    }
+}

+ 50 - 0
springboot-demo/src/main/java/com/sf/javase/thread/MyThread4.java

@@ -0,0 +1,50 @@
+package com.sf.javase.thread;
+
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+
+public class MyThread4 {
+    // 使用线程池
+    public static void main(String[] args) {
+        ThreadPoolExecutor executor = new ThreadPoolExecutor(
+                // 核心线程数 最大线程数 非核心线程保持连接的时间 时间单位 阻塞队列
+                5, 10, 200, TimeUnit.MILLISECONDS,
+                new ArrayBlockingQueue<>(3)
+        );
+        // 要执行的线程 是放在线程池中的任务
+        for (int i = 0; i < 14; i++) {
+            MyTask task = new MyTask(i);
+            // 将任务提交到线程池中
+            executor.execute(task);
+            // 当提交前5个任务时  会直接执行 占用核心线程
+            System.out.println("线程池中线程数量:" + executor.getPoolSize());
+            // 当提交第6个任务时  会存放在队列中
+            System.out.println("线程池中队列大小:" + executor.getQueue().size());
+            // 当核心线程都被占用  等待队列也满了 会创建非核心线程
+            // 非核心线程能创建多少 = 最大线程数 - 核心线程数 = 10 - 5 = 5
+            // 当所有线程都用满 队列也放满 此时线程池不能再接受任务  会拒绝(默认拒绝策略为 抛出异常)
+        }
+        executor.shutdown();
+    }
+}
+
+class MyTask implements Runnable {
+    int taskNum;
+
+    public MyTask(int taskNum) {
+        this.taskNum = taskNum;
+    }
+
+    @Override
+    public void run() {
+        System.out.println("当前正在执行taskNum=" + taskNum);
+        try {
+            Thread.sleep(4000);
+        } catch (InterruptedException e) {
+            throw new RuntimeException(e);
+        }
+        System.out.println("taskNum=" + taskNum + "执行完成");
+    }
+}
+

+ 45 - 0
springboot-demo/src/main/java/com/sf/javase/thread/MyThreadPool.java

@@ -0,0 +1,45 @@
+package com.sf.javase.thread;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Random;
+import java.util.concurrent.*;
+
+public class MyThreadPool {
+
+    public static void main(String[] args) throws ExecutionException, InterruptedException {
+        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
+                5,10,200, TimeUnit.MILLISECONDS,
+                new LinkedBlockingQueue<>(10)
+        );
+
+        List<Future<Integer>> futures = new ArrayList<>();
+        for (int i = 0; i < 10; i++) {
+            MyCallableTask myCallableTask = new MyCallableTask();
+            // execute主要处理的是runnable类型的任务
+            // 使用线程池的submit方法进行提交
+            // FutureTask<V> implements RunnableFuture<V>
+            // interface RunnableFuture<V> extends Runnable, Future<V>
+            // FutureTask既实现了Runnable 又实现了Future
+            Future<Integer> future = threadPoolExecutor.submit(myCallableTask);
+            futures.add(future);
+        }
+
+        for (Future<Integer> future : futures) {
+            Integer result = future.get();
+            System.out.println(result);
+        }
+
+        // 线程池是一个资源 需要手动关闭
+        threadPoolExecutor.shutdown();
+
+    }
+}
+
+class MyCallableTask implements Callable<Integer> {
+
+    @Override
+    public Integer call() throws Exception {
+        return new Random().nextInt(100);
+    }
+}