问题描述:

If I use Executors.newSingleThreadExecutor() to get an ExecutorService, can I set up the timeout for the execution of each one of the Runnables? If not, is there a default value? Or would be the Thread blocked if one of the runnables takes a long time to execute?

I've read the android documentation but it is not entirely clear to me (from the Executors.newSingleThreadExecutor() Android reference):

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 prior to shutdown, a new one will take its place if needed to execute subsequent tasks.) Tasks are guaranteed to execute sequentially, and no more than one task will be active at any given time. Unlike the otherwise equivalent newFixedThreadPool(1) the returned executor is guaranteed not to be reconfigurable to use additional threads.

网友答案:

What kind of Runnable is it? AFIK, if it is not a Future, then you can't set a timeout on it. You might also be confusing the timeout that is settable on a ThreadPoolExecutor (time for the pool to kill idle threads in order to come back down to core pool size) with that of an individual tasks's execution time.

相关阅读:
Top