多线程中使用ExcetorService的理解

java.util.concurrent包里提供了关于多线程操作的类,平常用的比较多的是ExecutorService及其实现类(如ThreadPoolExecutor等),Executor,Executors,Future,Callable等

1. ExecutorService(继承自Executor)接口:提供了一些异步的多线程操作方法,如execute(), submit(), shutdown(), shutdownNow()等

2. Executor接口:执行提交的任务(线程),只有一个方法 execute(Runnable a)

2. Executors类: 提供了一些工厂方法和一些公共方法来操作Executor子类和ThreadFactory等,如newXXX(),xxxThreadFactory()等

3. Futrue接口:代表了线程执行结果,提供了获取线程执行结果和取消线程的方法,如get(),cancle()等

4. Callable接口:JDK1.5提供的有返回值的线程执行新接口

对ExecutorService和Future的理解做简单记录

代码:

 1 public class Main {
 2     private static int count = 0;
 3 
 4     public static void main(String[] args){
 5         List<Future> resultList = new LinkedList<>();
 6 
 7         /**
 8          * Executors.newCachedThreadPool()  创建一个线程缓存池,若60s中线程没有被使用,则会停止线程并从缓存池中移除
 9          * Executors.newScheduledThreadPool() 创建一个固定容量的线程池,里边的线程按照设定的调度时间执行
10          * Executors.newFixedThreadPool()   拥有固定容量的线程缓存池
11          * Executors.newSingleThreadExecutor()  容量为一的线程缓存池,只会有一个线程
12          */
13         ExecutorService executorService = Executors.newCachedThreadPool();
14         for(int i=0; i<10; i++){
15             Future future = executorService.submit(new Callable<String>() {
16                 @Override
17                 public String call() {
18                     try {
19                         System.out.println(Thread.currentThread().getName());
20                         Thread.sleep(5000);
21                     } catch (InterruptedException e) {
22                         e.printStackTrace();
23                     }
24                     int count = Main.count;
25                     System.out.println(Thread.currentThread().getName() + "..start Main count:..." + count);
26                     Main.count = ++count;
27                     System.out.println(Thread.currentThread().getName() + "..end Main count:..." + Main.count);
28                     return Thread.currentThread().getName();
29                 }
30             });
31             resultList.add(future);
32         }
33         executorService.shutdown();
34         for(Future future: resultList){
35             try {
36                 System.out.println(future.get() + "..is over...");
37             } catch (InterruptedException e) {
38                 e.printStackTrace();
39             } catch (ExecutionException e) {
40                 e.printStackTrace();
41             }
42         }
43         System.out.println("main thread end...");
44     }
45 }

 

输出:

pool-1-thread-1
pool-1-thread-2
pool-1-thread-3
pool-1-thread-4
pool-1-thread-5
pool-1-thread-6
pool-1-thread-7
pool-1-thread-8
pool-1-thread-9
pool-1-thread-10
pool-1-thread-1..start Main count:...0
pool-1-thread-2..start Main count:...0
pool-1-thread-3..start Main count:...1
pool-1-thread-2..end Main count:...1
pool-1-thread-1..end Main count:...1
pool-1-thread-3..end Main count:...2
pool-1-thread-1..is over...
pool-1-thread-2..is over...
pool-1-thread-4..start Main count:...2
pool-1-thread-3..is over...
pool-1-thread-4..end Main count:...3
pool-1-thread-4..is over...
pool-1-thread-5..start Main count:...3
pool-1-thread-5..end Main count:...4
pool-1-thread-5..is over...
pool-1-thread-6..start Main count:...4
pool-1-thread-6..end Main count:...5
pool-1-thread-6..is over...
pool-1-thread-7..start Main count:...5
pool-1-thread-7..end Main count:...6
pool-1-thread-7..is over...
pool-1-thread-8..start Main count:...6
pool-1-thread-8..end Main count:...7
pool-1-thread-8..is over...
pool-1-thread-9..start Main count:...7
pool-1-thread-9..end Main count:...8
pool-1-thread-9..is over...
pool-1-thread-10..start Main count:...8
pool-1-thread-10..end Main count:...9
pool-1-thread-10..is over...
main thread end... //主线程在所有线程执行完成后结束

控制台在等待5秒后打印出上边的输出结果,原因是所有的线程启动的时候是一个并发操作,都会去等待5秒,所以整体看来只等了5秒,这是一个并发操作

总结:

1. ExecutorService提供的execute()方法和submit()方法的区别:

  a. execute()方法只接受Runnable类型的实例,所以不能拿到返回值,也不能动态获取线程执行的情况

  b. submit()方法接受Runnable和Callable实例,会返回Future实例,Future实例的get()方法可以获取线程执行返回值,并能抛出线程执行异常。所以如果要获取线程执行返回的结果,并能处理线程执行时可能出现的异常,或者想中途取消线程执行时可以使用submit()方法

2. 通过输出可以看到main方法(主线程)在所有线程执行完成后结束,原因:

  a. 通过submit()方法获取Future实例,并通过Future实例的get()方法获取线程返回结果,而Future实例的get()方法会等待线程执行完毕才会返回,所以main方法会等待所有子线程结束才会结束

  b. 若去掉上边红色标注的for循环,则main方法(主线程)会提前结束,而不会等待所有子线程结束

补充:

1. 多个线程并发执行时,若其中某一个线程出现了异常并且没有被处理,则该线程会自动停止执行,但其他线程还是会正常执行,这就是为什么tomcat请求出现异常时,tomcat还可以继续提供服务的原因

2. tomcat提供了线程池和等待池,每一个请求过来都会重新启动一个新的线程处理该请求,若线程池中线程用完,再来请求的时候就会放到等待池中等待,当其中有线程释放回线程池中时,就会为等待池中的请求分配线程处理请求

转载于:https://www.cnblogs.com/stefanking/articles/5071230.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值