Difference between Executors.newFixedThreadPool(1) and Executors.newSingleThreadExecutor()
Solution 1
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 Future
s), 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.
Solution 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.
Solution 3
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();
}
Related videos on Youtube
TheLostMind
Of all the things I've lost, I miss my mind the most :) Feel free to connect with me on LinkedIn
Updated on September 15, 2022Comments
-
TheLostMind over 1 year
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 callingnew 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!
-
TheLostMind over 10 yearsThanks for the answer.. What exactly do you mean by reconfigurable?
-
assylias over 10 years
((ThreadPoolExecutor) fixedThreadPool).setMaximumPoolSize(10);
and it's not a single thread pool any more. -
assylias over 10 yearsNo - it will reconfigure the existing one to allow more than one thread.
-
robinhowlett almost 8 yearsA potential downside of using
Executors.newSingleThreadExecutor()
is described in this blog post: farside.org.uk/201309/learning_from_bad_code. Recommended to useExecutors.newFixedThreadPool(1)
-
assylias almost 8 years@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.
-
android developer over 3 yearsWill any of these executors ditch the created threads when it seems they aren't needed for a long time? Is there such a thing?
-
assylias over 3 yearsDepends on which executor you use - but for example
Executors.newChachedThreadPool()
will act as you describe - see here: "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."