0

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.

  1. 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?

  2. 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 ?

Ravindra babu
  • 37,698
  • 11
  • 250
  • 211
  • 1
    "ThreadPoolExecutor wrapped by ExeuctorService" ThreadPoolExecutor *implements* ExecutorService, it isn't wrapped by it. – Andy Turner Oct 27 '15 at 08:11

1 Answers1

1
  1. 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.

  1. 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.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
  • Now I changed the second query. Does ThreadPoolExecutor wrapped by ExecutorService offers unbounded queue? – Ravindra babu Oct 27 '15 at 08:07
  • ExecutorService is an interface: it returns whatever the implementation decides to return; however, no method on ExecutorService returns an ExecutorService like a ThreadPoolExecutor. Assuming you really mean Executors, you should read the Javadoc, which makes it pretty clear. – Andy Turner Oct 27 '15 at 08:07
  • Got the answer for Unbounded queue. It's unbounded. Earlier I checked at wrong place. – Ravindra babu Oct 27 '15 at 08:11
  • public static ExecutorService newFixedThreadPool(int nThreads) Creates a thread pool that reuses a fixed number of threads operating off a shared unbounded queue. – Ravindra babu Oct 27 '15 at 08:21
  • I also thought of the same on usage of ThreadPoolExecutor. But after reading experts comments in other question I mentioned in the post, I got doubt that ExecutorService.getXXX is the right way. Thanks for your confirmation. – Ravindra babu Oct 27 '15 at 08:23