Question

My question is : does it make sense to use Executors.newFixedThreadPool(1)??. In two threads (main + oneAnotherThread) scenarios is it efficient to use executor service?. Is creating a new thread directly by calling new Runnable(){ } better than using ExecutorService?. What are the upsides and downsides of using ExecutorService for such scenarios?

PS: Main thread and oneAnotherThread dont access any common resource(s).

I have gone through : What are the advantages of using an ExecutorService?. and Only one thread at a time!

Was it helpful?

Solution

does it make sense to use Executors.newFixedThreadPool(1)?

It is essentially the same thing as an Executors.newSingleThreadExecutor() except that the latter is not reconfigurable, as indicated in the javadoc, whereas the former is if you cast it to a ThreadPoolExecutor.

In two threads (main + oneAnotherThread) scenarios is it efficient to use executor service?

An executor service is a very thin wrapper around a Thread that significantly facilitates the thread lifecycle management. If the only thing you need is to new Thread(runnable).start(); and move on, then there is no real need for an ExecutorService.

In any most real life cases, the possibility to monitor the life cycle of the tasks (through the returned Futures), the fact that the executor will re-create threads as required in case of uncaught exceptions, the performance gain of recycling threads vs. creating new ones etc. make the executor service a much more powerful solution at little additional cost.

Bottom line: I don't see any downsides of using an executor service vs. a thread.

The difference between Executors.newSingleThreadExecutor().execute(command) and new Thread(command).start(); goes through the small differences in behaviour between the two options.

OTHER TIPS

Sometimes need to use Executors.newFixedThreadPool(1) to determine number of tasks in the queue

private final ExecutorService executor = Executors.newFixedThreadPool(1);

public int getTaskInQueueCount() {
    ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) executor;
    return threadPoolExecutor.getQueue().size();
}

does it make sense to use Executors.newFixedThreadPool(1)??

Yes. It makes sense If you want to process all submitted tasks in order of arrival

In two threads (main + oneAnotherThread) scenarios is it efficient to use executor service? Is creating a new thread directly by calling new Runnable(){ } better than using ExecutorService?.

I prefer ExecutorService or ThreadPoolExecutor even for 1 thread.

Refer to below SE question for explanation for advantages of ThreadPoolExecutor over new Runnable() :

ExecutorService vs Casual Thread Spawner

What are the upsides and downsides of using ExecutorService for such scenarios?

Have a look at related SE question regarding ExexutorService use cases :

Java's Fork/Join vs ExecutorService - when to use which?

Regarding your query in subject line (from grepcode), both are same:

newFixedThreadPool API will return ThreadPoolExecutor as ExecutorService:

public static ExecutorService newFixedThreadPool(int nThreads) {
    return new ThreadPoolExecutor(nThreads, nThreads,
                                  0L, TimeUnit.MILLISECONDS,
                                  new LinkedBlockingQueue<Runnable>());

and

newSingleThreadExecutor() return ThreadPoolExecutor as ExecutorService:

public static ExecutorService newSingleThreadExecutor() {
    return new FinalizableDelegatedExecutorService
        (new ThreadPoolExecutor(1, 1,
                                0L, TimeUnit.MILLISECONDS,
                                new LinkedBlockingQueue<Runnable>()));

I agree with @assylias answer regarding similarities/differences.

Is creating a new thread directly by calling new Runnable(){ } better than using ExecutorService?

If you want to compute something on the returned result after thread compilation, you can use Callable interface, which can be used with ExecutorService only, not with new Runnable(){}. The ExecutorService's submit() method, which take the Callable object as an arguement, returns the Future object. On this Future object you check whether the task has been completed on not using isDone() method. Also you can get the results using get() method. In this case, ExecutorService is better than the new Runnable(){}.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top