user6253673
user6253673

Reputation: 27

newSingleThreadExecutor parallel use case

I have a question about how the newSingleThreadExecutor call works. I'm currently running a multi-threaded program. If I call newSingleThreadExecutor from within a pre-existing thread, will it halt the current thread to execute its task or run parallel to the thread, like a thread within a thread situation?

Upvotes: 0

Views: 1078

Answers (2)

Leo Aso
Leo Aso

Reputation: 12473

Executors.newSingleThreadExecutor() creates a new Thread and executes all tasks passed to it on that thread. It doesn't block the current thread - it wouldn't be of much use if it did.

It is called single thread because, if you execute multiple tasks on it, it does not create multiple threads. Instead, it waits for one task to complete before starting the next one on the same thread.

Upvotes: 2

Vinicius Falcão
Vinicius Falcão

Reputation: 32

Executors.newSingleThreadExecutor() will only give you a ExecutorService that provide methods for manage threads executions.

https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ExecutorService.html

The behavior of halt or not halt your current thread depends of your Threads Calls implementation.

Upvotes: 0

Related Questions