karra
karra

Reputation: 55

newSingleThreadExecutor() method in Executors Class

The definition for above method is " Creates an Executor that uses a single worker thread operating off an unbounded queue."

I have seen examples which uses above method for "execute multiple instances of a method with different inputs in parallel" by adding different tasks in data structure like "Set" or "List" and calling method invokeAll()

Here I am confused about one thing : If the above method creates only a single worker thread, How is it possible to run the tasks in parallel ?

Of course, there are other methods like newCachedThreadPool(), newFixedThreadPool(int numberOfThreads)- Here many number of threads are created to run in parallel, if needed.

Upvotes: 3

Views: 1520

Answers (1)

Krzysztof Cichocki
Krzysztof Cichocki

Reputation: 6414

It is not possible to run more than one task at a time using the newSingleThreadExecutor:

ExecutorService executor = Executors.newSingleThreadExecutor();

But, it has a work queue in which you can put lot of tasks, these tasks simply wait in the queue to be executed and are executed one by one by the single thread in this executor.

Upvotes: 1

Related Questions