bra*_*orm 53 java multithreading executorservice threadpool
我正在学习用来ExectorService
汇集threads
和发送任务.我有一个简单的程序如下
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
class Processor implements Runnable {
private int id;
public Processor(int id) {
this.id = id;
}
public void run() {
System.out.println("Starting: " + id);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
System.out.println("sorry, being interupted, good bye!");
System.out.println("Interrupted "+Thread.currentThread().getName());
e.printStackTrace();
}
System.out.println("Completed: " + id);
}
}
public class ExecutorExample {
public static void main(String[] args) {
Boolean isCompleted=false;
ExecutorService executor = Executors.newFixedThreadPool(2);
for(int i=0; i<5; i++) {
executor.execute(new Processor(i));
}
//executor does not accept any more tasks but the submitted tasks continue
executor.shutdown();
System.out.println("All tasks submitted.");
try {
//wait for the exectutor to terminate normally, which will return true
//if timeout happens, returns false, but this does NOT interrupt the threads
isCompleted=executor.awaitTermination(100, TimeUnit.SECONDS);
//this will interrupt thread it manages. catch the interrupted exception in the threads
//If not, threads will run forever and executor will never be able to shutdown.
executor.shutdownNow();
} catch (InterruptedException e) {
}
if (isCompleted){
System.out.println("All tasks completed.");
}
else {
System.out.println("Timeout "+Thread.currentThread().getName());
}
}
}
Run Code Online (Sandbox Code Playgroud)
它没有任何花哨,但创建了两个threads
并总共提交了5个任务.在每个thread
完成任务之后,它需要下一个,在上面的代码中,我使用executor.submit
.我也换了executor.execute
.但我没有看到输出有任何差异.这些submit and execute
方法有何不同?这API
就是说
方法submit通过创建和返回可用于取消执行和/或等待完成的Future来扩展基本方法Executor.execute(java.lang.Runnable).方法invokeAny和invokeAll执行最常用的批量执行形式,执行一组任务,然后等待至少一个或全部完成.(类ExecutorCompletionService可用于编写这些方法的自定义变体.)
但我不清楚它究竟意味着什么?
dka*_*zel 50
正如您从JavaDoc execute(Runnable)
中看到的那样,不会返回任何内容.
但是,submit(Callable<T>)
返回一个Future
对象,该对象允许您以某种方式以编程方式取消正在运行的线程,以及获取完成后T
返回的线程Callable
.有关更多详细信息,请参阅JavaDoc of Future
Future<?> future = executor.submit(longRunningJob);
...
//long running job is taking too long
future.cancel(true);
Run Code Online (Sandbox Code Playgroud)
此外,如果future.get() == null
并且没有抛出任何异常,则Runnable成功执行
tbo*_*odt 44
不同之处在于,execute
只需启动任务而不需要任何进一步的麻烦,而submit
返回一个Future
对象来管理任务.您可以使用该Future
对象执行以下操作:
cancel
方法提前取消任务.get
.Future
如果您向Callable
池提交,则该界面更有用.call
调用时将返回方法的返回值Future.get
.如果你没有保留对它的引用Future
,那就没有区别了.
Rav*_*abu 16
execute:
用它来点火和忘记电话
submit:
使用它来检查方法调用的结果,Future
并对调用返回的对象采取适当的操作
主要区别:Exception
处理
submit()
Exception
在框架本身隐藏未处理.
execute()
抛出未处理的Exception
.
用于处理异常的解决方案 submit()
包裹你的 Callable or Runnable code in try{} catch{} block
要么
保持 future.get() call in try{} catch{} block
要么
实现自己的ThreadPoolExecutor
和覆盖afterExecute
方法
关于旅游其他查询
执行给定的任务,返回一个Futures列表,在完成或超时到期时保持其状态和结果,以先发生者为准.
执行给定的任务,返回已成功完成的任务的结果(即,不抛出异常),如果在给定的超时之前已经执行了任何操作.
使用invokeAll
,如果你想等待所有提交的任务来完成.
invokeAny
如果您正在寻找N个提交任务中的一个任务的成功完成,请使用.在这种情况下,如果其中一个任务成功完成,则将取消正在进行的任务.
相关文章的代码示例:
在ExecutorService的提交和ExecutorService的执行之间进行选择
ami*_*788 10
submit() 和 execute() 方法的主要区别在于 ExecuterService.submit() 可以返回计算结果,因为它的返回类型为 Future,而 execute() 方法不能返回任何内容,因为它的返回类型为 void。Java 1.5 Executor 框架中的核心接口是Executor 接口,它定义了execute(Runnable task) 方法,其主要目的是将任务与其执行分离。
任何提交给 Executor 的任务都可以由同一个线程、线程池中的工作线程或任何其他线程执行。
另一方面,在Executor的子接口ExecutorService接口中定义了submit()方法,增加了终止线程池的功能,同时增加了submit()方法,可以接受Callable任务并返回结果的计算。
execute() 和 submit() 之间也有相似之处:
除了 submit() 方法可以返回输出而 execute() 方法不能返回,以下是 Java 5 Executor 框架的这两个关键方法之间的其他显着差异。
如果您检查源代码,您会发现它submit
是execute
.
java.util.concurrent.AbstractExecutorService#submit(java.lang.Runnable)
Run Code Online (Sandbox Code Playgroud)
public Future<?> submit(Runnable task) {
if (task == null) throw new NullPointerException();
RunnableFuture<Void> ftask = newTaskFor(task, null);
execute(ftask);
return ftask;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
70415 次 |
最近记录: |