引言
ExecutorService线程池执行线程时有submit 和execute 两种方法,它们两者之间有什么区别了?可能平时我们只是运用,没有深究其区别,在此我们可以看看它们的区别?
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