===============
== ffff65535 ==
===============
十六个一[1111111111111111]

线程的三种创建方式

thread

继承Thread类

  1. 继承Thread类型
  2. 重写run方法
  3. 调用start开启多线程

线程的执行顺序无法控制,由CPU调度执行

public class TestThread extends Thread {
    public static void main(String[] args) {
        // 创建一个线程对象,调用start()方法
        TestThread testThread = new TestThread();
        // 调用start方法开启多线程(观察打印日志会发现:run方法和main方法交替执行)
        testThread.start();
        // 调用run方法,(观察打印日志会发现:会先执行run方法,再执行main方法)
        //testThread.run();
        // 主线程
        for (int i = 0; i < 200; i++) {
            System.out.println("main方法线程--" + i);
        }
    }

    /**
     * 1.继承Thread类型
     * 2.重写run方法
     */
    @Override
    public void run() {
        super.run();
        // run方法线程体
        for (int i = 0; i < 200; i++) {
            System.out.println("run方法线程--" + i);
        }
    }

}

参考资料:

https://www.bilibili.com/video/BV1V4411p7EF?p=3

实现Runnable接口

  1. 实现Runnable接口
  2. 重写run方法
  3. 创建线程对象(Thread),通过线程对象来开启线程

Thread 类也是 Runnable 接口的子类

public class TestRunnable implements Runnable {
    /**
     * 1.实现Runnable接口
     * 2.重写run方法
     * 3.创建线程对象,通过线程对象来开启我们的线程,代理
     */
    public static void main(String[] args) {
        // 创建Runnable接口的实现类
        TestRunnable testRunnable = new TestRunnable();
        // 创建线程对象,通过线程对象来开启我们的线程,代理
        Thread thread = new Thread(testRunnable);
        // 调用start方法开启多线程
        thread.start();
        // 主线程
        for (int i = 0; i < 2000; i++) {
            System.out.println("main方法线程--" + i);
        }
    }

    @Override
    public void run() {
        // run方法线程体
        for (int i = 0; i < 2000; i++) {
            System.out.println("run方法线程--" + i);
        }
    }
}

参考资料:

https://www.bilibili.com/video/BV1V4411p7EF?p=5

实现Callable接口

  1. 实现Callable接口,需要返回值类型public class TestCallable implements Callable<Boolean>
  2. 重写call方法,需要抛出异常@Override public Boolean call() {return true;}
  3. 创建目标对象TestCallable t1 = new TestCallable();
  4. 创建执行服务:ExecutorService executorService = Executors.newFixedThreadPool(3);
  5. 提交执行:Future<Boolean> submit1 = executorService.submit(t1);
  6. 获取结果:Boolean b1 = submit1.get();
  7. 关闭服务:executorService.shutdownNow();
public class TestCallable implements Callable<Boolean> {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // 创建TestCallable接口的实现类
        TestCallable t1 = new TestCallable();
        TestCallable t2 = new TestCallable();
        TestCallable t3 = new TestCallable();
        // 创建执行服务(创建一个包含3个线程的线程池)
        ExecutorService executorService = Executors.newFixedThreadPool(3);
        // 提交执行
        Future<Boolean> submit1 = executorService.submit(t1);
        Future<Boolean> submit2 = executorService.submit(t2);
        Future<Boolean> submit3 = executorService.submit(t3);

        // 获取结果
        Boolean b1 = submit1.get();
        Boolean b2 = submit2.get();
        Boolean b3 = submit3.get();

        // 关闭服务
        executorService.shutdownNow();
    }

    //实现Callable接口
    @Override
    public Boolean call() {
        for (int i = 0; i < 200; i++) {
            System.out.println(Thread.currentThread().getName() + "call方法线程--" + i);
        }
        return true;
    }
}

参考资料:

https://www.bilibili.com/video/BV1V4411p7EF?p=8