Java 创建线程的四种方式

你的名字 2020-06-14 10:09 1085阅读 0赞

继承Thread类

步骤

  1. 定义一个Thread类的子类,重写run方法,将相关逻辑实现,run()方法就是线程要执行的业务逻辑方法
  2. 创建自定义的线程子类对象
  3. 调用子类实例的star()方法来启动线程

    public class MyThread extends Thread {

    1. @Override
    2. public void run() {
    3. System.out.println(Thread.currentThread().getName() + " run()方法正在执行...");
    4. }

    }

    public class TheadTest {

    1. public static void main(String[] args) {
    2. MyThread myThread = new MyThread();
    3. myThread.start();
    4. System.out.println(Thread.currentThread().getName() + " main()方法执行结束");
    5. }

    }

运行结果

  1. main main()方法执行结束
  2. Thread-0 run()方法正在执行...

实现Runnable接口

步骤

  1. 定义Runnable接口实现类MyRunnable,并重写run()方法
  2. 创建MyRunnable实例myRunnable,以myRunnable作为target创建Thead对象,该Thread对象才是真正的线程对象
  3. 调用线程对象的start()方法

    public class MyRunnable implements Runnable {

    1. @Override
    2. public void run() {
    3. System.out.println(Thread.currentThread().getName() + " run()方法执行中...");
    4. }

    }

    public class RunnableTest {

    1. public static void main(String[] args) {
    2. MyRunnable myRunnable = new MyRunnable();
    3. Thread thread = new Thread(myRunnable);
    4. thread.start();
    5. System.out.println(Thread.currentThread().getName() + " main()方法执行完成");
    6. }

    }

执行结果

  1. main main()方法执行完成
  2. Thread-0 run()方法执行中...

使用Callable和Future创建线程

步骤

  1. 创建实现Callable接口的类myCallable
  2. 以myCallable为参数创建FutureTask对象
  3. 将FutureTask作为参数创建Thread对象
  4. 调用线程对象的start()方法

    public class MyCallable implements Callable {

    1. @Override
    2. public Integer call() {
    3. System.out.println(Thread.currentThread().getName() + " call()方法执行中...");
    4. return 1;
    5. }

    }

    public class CallableTest {

    1. public static void main(String[] args) {
    2. FutureTask<Integer> futureTask = new FutureTask<Integer>(new MyCallable());
    3. Thread thread = new Thread(futureTask);
    4. thread.start();
    5. try {
    6. Thread.sleep(1000);
    7. System.out.println("返回结果 " + futureTask.get());
    8. } catch (InterruptedException e) {
    9. e.printStackTrace();
    10. } catch (ExecutionException e) {
    11. e.printStackTrace();
    12. }
    13. System.out.println(Thread.currentThread().getName() + " main()方法执行完成");
    14. }

    }

执行结果

  1. Thread-0 call()方法执行中...
  2. 返回结果 1
  3. main main()方法执行完成

使用Executor框架创建线程池

Executors提供了一系列工厂方法用于创先线程池,返回的线程池都实现了ExecutorService接口。

主要有newFixedThreadPool,newCachedThreadPool,newSingleThreadExecutor,newScheduledThreadPool,后续详细介绍这四种线程池

  1. public class MyRunnable implements Runnable {
  2. @Override
  3. public void run() {
  4. System.out.println(Thread.currentThread().getName() + " run()方法执行中...");
  5. }
  6. }
  7. public class SingleThreadExecutorTest {
  8. public static void main(String[] args) {
  9. ExecutorService executorService = Executors.newSingleThreadExecutor();
  10. MyRunnable runnableTest = new MyRunnable();
  11. for (int i = 0; i < 5; i++) {
  12. executorService.execute(runnableTest);
  13. }
  14. System.out.println("线程任务开始执行");
  15. executorService.shutdown();
  16. }
  17. }

执行结果

  1. 线程任务开始执行
  2. pool-1-thread-1 is running...
  3. pool-1-thread-1 is running...
  4. pool-1-thread-1 is running...
  5. pool-1-thread-1 is running...
  6. pool-1-thread-1 is running...

发表评论

表情:
评论列表 (有 0 条评论,1085人围观)

还没有评论,来说两句吧...

相关阅读