首页 > 其他分享 >线程池ExecutorService中submit 和execute的区别

线程池ExecutorService中submit 和execute的区别

时间:2022-11-11 13:46:00浏览次数:72  
标签:execute executorService System submit future 线程 println ExecutorService out

引言

     ExecutorService线程池执行线程时有submitexecute 两种方法,它们两者之间有什么区别了?可能平时我们只是运用,没有深究其区别,在此我们可以看看它们的区别?

   1:executorService.execute()   

 1 public void execute(){
 2         ExecutorService executorService = Executors.newSingleThreadExecutor();
 3         executorService.execute(()->{
 4             try {
 5                 for (int i = 0; i < 10; i++) {
 6                     Book book = new Book().setId("test" + (i + 1)).setName("平凡的世界").setAuthor("路遥").setContent("孙少安和田晓霞");
 7                     System.out.println("===book: " + JSONObject.toJSONString(book));
 8                     Thread.sleep(50);
 9                 }
10             } catch (InterruptedException e) {
11                 e.printStackTrace();
12             }
13         });
14         executorService.shutdown();
15     }

     无返回值,直接执行,因此不能阻塞线程,并且在执行异常时会抛出异常

 2:executorService.submit()方法

      有三种类型,返回future。    可以看ExecutorService接口的源码,submit有三种调用方式,Callable回调、result结果、Runnable

public interface ExecutorService extends Executor {
    
    <T> Future<T> submit(Callable<T> task);
    
    <T> Future<T> submit(Runnable task, T result);
    
    Future<?> submit(Runnable task);
    
}

      2.1: 带Callable回调函数的返回

 1 public void submit1() throws ExecutionException, InterruptedException {
 2         Callable<String> callable  = new Callable<String>() {
 3             @Override
 4             public String call() throws Exception {
 5                 return Thread.currentThread().getName()+"==== hello Callable";
 6             }
 7         };
 8         ExecutorService executorService = Executors.newSingleThreadExecutor();
 9         Future<String> future = executorService.submit(callable);
10         String s = executorService.submit(callable).get();
11         System.err.println(s);
12         System.out.println("======执行结果======"+future.get());
13         executorService.shutdown();
14     }

   返回结果:  

  

 

   2.2: 带T result) 返回结果的

 1 public void submit2() throws ExecutionException, InterruptedException {
 2         ExecutorService executorService = Executors.newSingleThreadExecutor();
 3         Future<?> future = executorService.submit(() -> {
 4             try {
 5                     Book book = new Book().setId("test").setName("平凡的世界").setAuthor("路遥").setContent("孙少安和田晓霞");
 6                     System.out.println("===book: " + JSONObject.toJSONString(book));
 7             } catch (Exception e) {
 8                 e.printStackTrace();
 9             }
10         },"返回结果了!");
11         executorService.shutdown();
12         Object o = future.get();
13         System.out.println("=====执行结果======"+o);
14         System.out.println("=====isDone======"+future.isDone());
15         System.out.println("=====cancel======"+future.cancel(true));
16         System.out.println("=====isCancelled======"+future.isCancelled());
17     }

   返回结果:  

 

  2.3:(Runnable task);  //不带返回结果

 1 public void submit3() throws ExecutionException, InterruptedException {
 2         ExecutorService executorService = Executors.newSingleThreadExecutor();
 3         Future<?> future = executorService.submit(() -> {
 4             try {
 5                     Book book = new Book().setId("test").setName("平凡的世界").setAuthor("路遥").setContent("孙少安和田晓霞");
 6                     System.out.println("===book: " + JSONObject.toJSONString(book));
 7             } catch (Exception e) {
 8                 e.printStackTrace();
 9             }
10         });
11         executorService.shutdown();
12         Object o = future.get();
13         System.out.println("=====执行结果======"+o);
14         System.out.println("=====isDone======"+future.isDone());
15         System.out.println("=====cancel======"+future.cancel(true));
16         System.out.println("=====isCancelled======"+future.isCancelled());
17     }

     返回结果:  

总结:

ExecutorService 的submit和execute的区别?
1: 接受的参数(任务类型)不一样。 execute只能接受Runnable类型的任务,submit不管是Runnable还是Callable类型的任务都可以接受,但是Runnable返回值均为void,所以使用Future的get()获得的还是null
2:返回值。execute没有返回值,submit有返回值,所以需要返回值的时候必须使用submit
3:异常的处理不同。
execute无返回值,直接执行,因此不能阻塞线程,并且在执行异常时会抛出异常
submit 有三种类型,返回future,可以阻塞获取线程执行结果,在执行异常时会被异常处理吃掉,不会抛出异常

 

标签:execute,executorService,System,submit,future,线程,println,ExecutorService,out
From: https://www.cnblogs.com/zhaosq/p/16880234.html

相关文章

  • 【Java】多线程 数目
    今天看到一篇文章,讲多线程数目的,很棒这个问题还是很容易被忽略的,就是多线程到底是为了什么?最开始学习多线程的时候,往往将多线程和性能高划等号,只要用了多线程就能提升性能,其......
  • 线程同步和死锁
    线程同步线程同步其实就是一种等待机制,多个需要同时访问此对象的线程进入这个对象的等待池形成队列,等待前面线程使用完毕,下一个线程再使用。synchronized锁为了保证多线......
  • 读者-写者(多线程)
    1.描述操作系统中“读者-写者”问题,理解问题的本质,提交你理解或查找到的文本资料问题描述:多个进程访问一个共享的数据区读者(读进程)只能读数据,写者(写进程)只能写数据......
  • Java多线程 CompletionService和ExecutorCompletionService
    目录​​一、说明​​​​二、理解​​​​三、实现​​​​1.使用Future​​​​2.使用ExecutorCompletionService​​​​3.take()方法​​​​4.poll()方法​​​​5.pol......
  • Java多线程 Callable和Future
    目录​​一、说明​​​​二、理解​​​​三、实现​​​​1.实现接口​​​​2.执行线程​​一、说明Java提供了三种创建线程的方法实现​​Runnable​​接口继承​​T......
  • Java多线程 Future和FutureTask的区别
    目录​​一、说明​​​​二、理解​​​​三、实现​​​​1.实现接口​​​​2.使用Future​​​​3.使用FutureTask​​一、说明Future和FutureTask的关系Future是一个......
  • Java多线程 ThreadPoolExecutor-RejectedExecutionHandler拒绝执行策略
    目录​​一、说明​​​​二、理解​​​​三、实现​​​​1.AbortPolicy​​​​2.DiscardPolicy​​​​3.DiscardOldestPolicy​​​​4.CallerRunsPolicy​​​​5.自......
  • Java多线程 线程池Executor框架
    目录​​一、说明​​​​二、理解​​​​Executor​​​​ExecutorService​​​​Executors​​​​三、实现​​​​1.newSingleThreadExecutor​​​​2.newFixedThr......
  • 定位java程序中占用cpu最高的线程堆栈信息
    找出占用cpu最高的线程堆栈信息在java编码中,有时会因为粗心导致cpu占用较高的情况,为了避免影响程序的正常运行,需要找到问题并解决。这里模拟一个cpu占用较高的场景,并尝试......
  • Executor 线程池原理
    线程池的创建publicThreadPoolExecutor(intcorePoolSize,//核心线程数intmaximumPoolSize,//最大线程数longkeepAli......