ThreadPoolExecutor 与 ExecutorService 的服务超时用例
ThreadPoolExecutor Vs ExecutorService for service time out use cases
我将在两个服务之间实现超时框架。我正在查看 ThreadPoolExecutor
VS ExecutorService
的 优缺点
使用 ExecutorService 编写代码。
ExecutorService service = Executors.newFixedThreadPool(10);
for ( int i=0; i<10; i++){
MyCallable myCallable = new MyCallable((long)i);
Future<Long> futureResult = service.submit(myCallable);
Long result = null;
try{
result = futureResult.get(5000, TimeUnit.MILLISECONDS);
}catch(TimeoutException e){
System.out.println("Time out after 5 seconds");
futureResult.cancel(true);
}catch(InterruptedException ie){
System.out.println("Error: Interrupted");
}catch(ExecutionException ee){
System.out.println("Error: Execution interrupted");
}
System.out.println("Result:"+result);
}
service.shutdown();
MyCallable 的代码片段
class MyCallable implements Callable{
Long id = 0L;
public MyCallable(Long val){
this.id = val;
}
public Long call(){
// **Call a service and get id from the service**
return id;
}
}
如果我想用ThreadPoolExecutor
实现,我会这样编码
/* Thread pool Executor */
BlockingQueue queue = new ArrayBlockingQueue(300);
ThreadPoolExecutor eventsExecutor =
new ThreadPoolExecutor(1, 10, 60,
TimeUnit.SECONDS, queue, new MyRejectionHandler());
/* I can submit the tasks as for above code example used in future */
现在我正在研究使用 ThreadPoolExecutor
与 ExecutorService
的优缺点。请不要认为此问题与 ExectuorService vs ThreadPoolExecutor (which is using LinkedBlockingQueue).
重复
阅读上述问题后我有一些疑问,因此发布了这个问题。
建议使用 ExecutorSevice
和 Executors.XXX methods
。如果我使用 Executors.XXX()
方法,我是否可以设置 RejectionHandler
、BlockingQueue
大小等?如果不是,我是否必须求助于 ThreadPoolExecutor
?
ExeuctorService
实现的ThreadPoolExecutor
是否提供无限队列?我正在两个服务之间实施 Timeout
框架。
这两者之间,哪一个是最好的选择?或者我有其他最佳选择吗?
- It was recommended to use ExecutorSevice with Executors.XXX methods. If I use Executors.XXX() methods, do I have capabilities to set RejectionHandler, BlockingQueue size etc? If not, Do I have to fall back on ThreadPoolExecutor?
不,您不能通过 Executors
工厂方法指定这些东西。但是,请看一下 Executors
的源代码:您会看到它的 newXXX
方法简单地包装调用以创建 ThreadPoolExecutor
个实例。
因此,使用 Executors
并没有什么特别的优势,除了可以方便地不必指定许多参数。如果您需要指定这些附加功能,则需要直接创建 ThreadPoolExecutor
个实例。
- Does ExeuctorService offers unbounded queue? I am implementing Timeout framework between two services. Which one is best option between these two? Or Do I have other best option (e.g. CountDownLatch etc.)
ExecutorService
是一个接口:它不提供任何实现细节,例如无限队列。
我将在两个服务之间实现超时框架。我正在查看 ThreadPoolExecutor
VS ExecutorService
使用 ExecutorService 编写代码。
ExecutorService service = Executors.newFixedThreadPool(10);
for ( int i=0; i<10; i++){
MyCallable myCallable = new MyCallable((long)i);
Future<Long> futureResult = service.submit(myCallable);
Long result = null;
try{
result = futureResult.get(5000, TimeUnit.MILLISECONDS);
}catch(TimeoutException e){
System.out.println("Time out after 5 seconds");
futureResult.cancel(true);
}catch(InterruptedException ie){
System.out.println("Error: Interrupted");
}catch(ExecutionException ee){
System.out.println("Error: Execution interrupted");
}
System.out.println("Result:"+result);
}
service.shutdown();
MyCallable 的代码片段
class MyCallable implements Callable{
Long id = 0L;
public MyCallable(Long val){
this.id = val;
}
public Long call(){
// **Call a service and get id from the service**
return id;
}
}
如果我想用ThreadPoolExecutor
实现,我会这样编码
/* Thread pool Executor */
BlockingQueue queue = new ArrayBlockingQueue(300);
ThreadPoolExecutor eventsExecutor =
new ThreadPoolExecutor(1, 10, 60,
TimeUnit.SECONDS, queue, new MyRejectionHandler());
/* I can submit the tasks as for above code example used in future */
现在我正在研究使用 ThreadPoolExecutor
与 ExecutorService
的优缺点。请不要认为此问题与 ExectuorService vs ThreadPoolExecutor (which is using LinkedBlockingQueue).
阅读上述问题后我有一些疑问,因此发布了这个问题。
建议使用
ExecutorSevice
和Executors.XXX methods
。如果我使用Executors.XXX()
方法,我是否可以设置RejectionHandler
、BlockingQueue
大小等?如果不是,我是否必须求助于ThreadPoolExecutor
?ExeuctorService
实现的ThreadPoolExecutor
是否提供无限队列?我正在两个服务之间实施Timeout
框架。
这两者之间,哪一个是最好的选择?或者我有其他最佳选择吗?
- It was recommended to use ExecutorSevice with Executors.XXX methods. If I use Executors.XXX() methods, do I have capabilities to set RejectionHandler, BlockingQueue size etc? If not, Do I have to fall back on ThreadPoolExecutor?
不,您不能通过 Executors
工厂方法指定这些东西。但是,请看一下 Executors
的源代码:您会看到它的 newXXX
方法简单地包装调用以创建 ThreadPoolExecutor
个实例。
因此,使用 Executors
并没有什么特别的优势,除了可以方便地不必指定许多参数。如果您需要指定这些附加功能,则需要直接创建 ThreadPoolExecutor
个实例。
- Does ExeuctorService offers unbounded queue? I am implementing Timeout framework between two services. Which one is best option between these two? Or Do I have other best option (e.g. CountDownLatch etc.)
ExecutorService
是一个接口:它不提供任何实现细节,例如无限队列。