好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Java并发编程创建并运行线程的方法对比

一、创建并运行线程的五种方法

第一种:继承Thread类

这种方式是最基础的一种方式,学过java的朋友都知道,不做赘述。需要注意的是: 覆盖实现使用的是run方法,运行线程是start方法。

?

1

2

3

4

5

6

7

8

9

10

public class FirstWay extends Thread  {

     @Override

     public void run() {

         System.out.println( "第一种实现线程的方式:继承Thread类" );

     }

     //模拟测试

     public static void main(String[] args) {

         new FirstWay().start();

     }

}

第二种:实现Runnable接口

第二种实现方式仍然很基础,继承Runnable接口,重写run方法实现线程运行逻辑。需要注意的:运行线程需要套一层 new Thread  。

?

1

2

3

4

5

6

7

8

9

10

public class SecondWay implements Runnable{

     @Override

     public void run() {

         System.out.println( "第二种实现线程的方式:实现Runnable接口" );

     }

     //模拟测试

     public static void main(String[] args) {

         new Thread( new SecondWay()).start();

     }

}

第三种:实现Callable接口

第三种方式是实现Callable接口,Callable接口与Runable接口都能实现线程。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

public class ThirdWay implements Callable<String> {

     @Override

     public String call() throws Exception {

         System.out.println( "第三种实现线程的方式:实现Callable接口" );

         return "Callable接口带返回值,可以抛出异常" ;

     }

 

     //模拟测试

     public static void main(String[] args) throws ExecutionException, InterruptedException {

         FutureTask<String> futureTask = new FutureTask<>( new ThirdWay());

         new Thread(futureTask).start();

         //阻塞方法,获取call方法返回值

         System.out.println(futureTask.get());  //打印:Callable接口带返回值,可以抛出异常

     }

}

区别如下:

Callable接口实现线程方法是call, Runable接口实现线程方法是run Callable有返回值, Runable接口不能有返回值 Callable接口方法call返回值可以设置泛型,如下例子中使用String数据类型 Callable接口方法call方法可以抛出异常,Runable接口run方法不可以抛出异常 Callable接口方法通过 new Thread(futureTask).start() 运行,FutureTask的get方法可以获取Callable接口方法call方法的返回值 如果Callable接口方法call方法异常,在FutureTask的get方法调用时也会抛出同样的异常

第四种:线程池 + execute

从JDK5版本开始,java默认提供了线程池的支持,用线程池的方式运行线程可以避免线程的无限扩张导致应用宕机,同时也节省了线程频繁创建与销毁的资源与时间成本。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

public class FourthWay implements Runnable{

     @Override

     public void run() {

         System.out.println(Thread.currentThread().getName() +

                 ":实现线程的方式Runnable接口,但运行方式不一样,使用线程池" );

     }

 

     public static void main(String[] args) {

         //创建一个固定大小的线程池

         ExecutorService threadPool = Executors.newFixedThreadPool( 5 );

         for ( int i = 0 ;i < 10 ;i++){

             threadPool.execute( new FourthWay());

         }

     }

}

线程池ExecutorService使用execute方法运行Runnable接口run方法的线程实现,execute方法与run方法的共同特点是没有返回值。

?

1

2

3

4

5

6

7

8

9

10

pool-1-thread-5:实现线程的方式Runnable接口,但运行方式不一样,使用线程池

pool-1-thread-2:实现线程的方式Runnable接口,但运行方式不一样,使用线程池

pool-1-thread-4:实现线程的方式Runnable接口,但运行方式不一样,使用线程池

pool-1-thread-4:实现线程的方式Runnable接口,但运行方式不一样,使用线程池

pool-1-thread-4:实现线程的方式Runnable接口,但运行方式不一样,使用线程池

pool-1-thread-1:实现线程的方式Runnable接口,但运行方式不一样,使用线程池

pool-1-thread-4:实现线程的方式Runnable接口,但运行方式不一样,使用线程池

pool-1-thread-3:实现线程的方式Runnable接口,但运行方式不一样,使用线程池

pool-1-thread-2:实现线程的方式Runnable接口,但运行方式不一样,使用线程池

pool-1-thread-5:实现线程的方式Runnable接口,但运行方式不一样,使用线程池

从上面的结果中可以看出,线程池中包含五个线程。线程运行完成之后并不销毁,而是还回到线程池,下一次执行时从线程池中获取线程资源再次运行。

第五种:线程池 + submit

下面的例子线程池ExecutorService使用submit方法运行Callable接口call方法的线程实现,submit方法与call方法的共同特点是存在返回值。

Callable接口call方法的返回值可以由泛型定义 ExecutorService线程池submit方法的返回值是Future

Future的get方法可以获取call方法的返回值,同时 如果call方法抛出异常,Future的get方法也会抛出异常。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

public class FifthWay implements Callable<String> {

     @Override

     public String call() throws Exception {

         return Thread.currentThread().getName() + ":Callable接口带返回值,可以抛出异常" ;

     }

 

     //模拟测试

     public static void main(String[] args) throws ExecutionException, InterruptedException {

         //保存多线程执行结果

         List<String> retList = new ArrayList<>();

         //创建一个固定大小的线程池

         ExecutorService threadPool = Executors.newFixedThreadPool( 5 );

         for ( int i = 0 ;i < 10 ;i++){

             Future<String> future = threadPool.submit( new FifthWay());

             retList.add(future.get());

         }

         //java8 语法,打印retlist

         retList.forEach(System.out::println);

     }

}

上文代码中有一个小小的语法糖, retList.forEach(System.out::println); 是java8提供的方法引用

?

1

2

3

4

5

6

7

8

9

10

pool-1-thread-1:Callable接口带返回值,可以抛出异常

pool-1-thread-2:Callable接口带返回值,可以抛出异常

pool-1-thread-3:Callable接口带返回值,可以抛出异常

pool-1-thread-4:Callable接口带返回值,可以抛出异常

pool-1-thread-5:Callable接口带返回值,可以抛出异常

pool-1-thread-1:Callable接口带返回值,可以抛出异常

pool-1-thread-2:Callable接口带返回值,可以抛出异常

pool-1-thread-3:Callable接口带返回值,可以抛出异常

pool-1-thread-4:Callable接口带返回值,可以抛出异常

pool-1-thread-5:Callable接口带返回值,可以抛出异常

总结

本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注的更多内容!     

原文链接:https://blog.csdn.net/hanxiaotongtong/article/details/123541225

查看更多关于Java并发编程创建并运行线程的方法对比的详细内容...

  阅读:23次