Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Executors.newFixedThreadPool(1) and Executors.newSingleThreadExecutor()

Tags:

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!

like image 234
TheLostMind Avatar asked Jan 23 '14 06:01

TheLostMind


People also ask

What is Executors newFixedThreadPool?

The newFixedThreadPool() method of Executors class creates a thread pool that reuses a fixed number of threads operating off a shared unbounded queue. At any point, at most n Threads will be active processing tasks.

What's the difference between Cachedpool and Fixedthreadpool of Executor service?

As opposed to the cached thread pool, this one is using an unbounded queue with a fixed number of never-expiring threads. Therefore, instead of an ever-increasing number of threads, the fixed thread pool tries to execute incoming tasks with a fixed amount of threads.

What is newSingleThreadExecutor?

The newSingleThreadExecutor() method of Executors class creates an Executor that uses a single worker thread operating off an unbounded queue. (Note however that if this single thread terminates due to a failure during execution before the shutdown, a new one will take its place if needed to execute subsequent tasks.).

What are Executor and ExecutorService What are the differences between these interfaces?

The main difference between Executor, ExecutorService, and Executors class is that Executor is the core interface which is an abstraction for parallel execution. It separates tasks from execution, this is different from java. lang. Thread class which combines both task and its execution.


2 Answers

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.

like image 199
assylias Avatar answered Oct 06 '22 09:10

assylias


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(); } 
like image 25
v.ladynev Avatar answered Oct 06 '22 09:10

v.ladynev