27

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!

Community
  • 1
  • 1
TheLostMind
  • 35,966
  • 12
  • 68
  • 104

4 Answers4

26

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.

Community
  • 1
  • 1
assylias
  • 321,522
  • 82
  • 660
  • 783
  • 1
    Thanks for the answer.. What exactly do you mean by reconfigurable? – TheLostMind Jan 23 '14 at 06:40
  • 5
    `((ThreadPoolExecutor) fixedThreadPool).setMaximumPoolSize(10);` and it's not a single thread pool any more. – assylias Jan 23 '14 at 06:40
  • 1
    No - it will reconfigure the existing one to allow more than one thread. – assylias Jan 23 '14 at 06:42
  • A potential downside of using `Executors.newSingleThreadExecutor()` is described in this blog post: https://www.farside.org.uk/201309/learning_from_bad_code. Recommended to use `Executors.newFixedThreadPool(1)` – robinhowlett Jun 16 '16 at 22:59
  • @robinhowlett interesting - although the issue described there only appears if you let go the reference to the executor without having called shutdown, which is probably a bug in the first instance. You won't get the issue if you keep a reference to the executor in a field and properly manage its lifecycle. – assylias Jun 17 '16 at 07:59
  • Will any of these executors ditch the created threads when it seems they aren't needed for a long time? Is there such a thing? – android developer Oct 07 '20 at 11:24
  • Depends on which executor you use - but for example `Executors.newChachedThreadPool()` will act as you describe - see [here](https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Executors.html#newCachedThreadPool--): "*Threads that have not been used for sixty seconds are terminated and removed from the cache. Thus, a pool that remains idle for long enough will not consume any resources.*" – assylias Oct 08 '20 at 08:25
2

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();
}
v.ladynev
  • 19,275
  • 8
  • 46
  • 67
2

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.

Community
  • 1
  • 1
Ravindra babu
  • 37,698
  • 11
  • 250
  • 211
0

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(){}.

Community
  • 1
  • 1
KayV
  • 12,987
  • 11
  • 98
  • 148