Reputation: 38910
I am going to implement Timeout framework between two services. I am looking at pros & cons of ThreadPoolExecutor
VS ExecutorService
Code with 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();
Code snippet for 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;
}
}
If I want to implement with ThreadPoolExecutor
, I will code in this way
/* 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 */
Now I am looking at pros & cons of using ThreadPoolExecutor
Vs ExecutorService
. Please don't think that this question is duplicate of ExectuorService vs ThreadPoolExecutor (which is using LinkedBlockingQueue).
I have some queries after reading above question and hence posted this question.
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
?
Does ThreadPoolExecutor
implemented by 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 ?
Upvotes: 0
Views: 3172
Reputation: 140318
- 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?
No, you can't specify these things via Executors
factory methods. However, take a look at the source code of Executors
: you will see that its newXXX
methods simply wrap calls to create ThreadPoolExecutor
instances.
As such, there is no particular advantage to using Executors
, aside from the convenience of not having to specify many of the parameters. If you need to specify these additional capabilities, you will need to create the ThreadPoolExecutor
instances directly.
- 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
is an interface: it offers you nothing by way of implementation details such as unbounded queues.
Upvotes: 1