Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExecutorService might execute on calling thread?

Tags:

java

android

If you read the documentation for Executor.execute, it says:

The command may execute in a new thread, in a pooled thread, or in the calling thread, at the discretion of the Executor implementation.

My understanding is that, by calling thread, they mean the thread that calls the execute command.

This has never happened in my experience, so my question is: Does this really ever happen?

Since the calling Thread controls the UI, I cannot allow additional work on the calling Thread. I am particularly concerned about whether this could happen when I construct the ExecutorService using the Executors.newFixedThreadPool(threads) method.

like image 306
bremen_matt Avatar asked Oct 22 '25 16:10

bremen_matt


1 Answers

ExecutorService is an interface which defines a broad contract. If you look at implementations, say ThreadPoolExecutor for example, you will have more precise characteristics:

An ExecutorService that executes each submitted task using one of possibly several pooled threads, normally configured using Executors factory methods.

Note that the javadoc linked above details the fact that the ExecutorServices returned by the various Executors factory methods are ThreadPoolExecutors.

So to answer your question more directly: Executors.newFixedThreadPool(threads) will not execute tasks on the calling thread.

like image 140
assylias Avatar answered Oct 24 '25 07:10

assylias



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!