Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android, Java: ExecutorService execute after shutdown

Tags:

java

android

I have a ExecutorService:

private final ExecutorService listenDataExecutorService = 
                              Executors.newSingleThreadExecutor();

In same case I need executor to stop doing his job. For this I call listenDataExecutorService.shutdown();

Then after some time, I need this executor to do this job again. But when I call listenDataExecutorService.execute() exception is thrown.

How can I execute new task for executor after shutdown ?

like image 948
Jim Avatar asked Oct 01 '14 14:10

Jim


Video Answer


1 Answers

Once an executor service has been shut down it can't be reactivated. Create a new executor service to restart execution:

listenDataExecutorService = Executors.newSingleThreadExecutor();

(You'd have to drop the final modifier though.)

Another option is to cancel all pending tasks instead, and then reschedule them when you want to resume execution.

like image 90
aioobe Avatar answered Oct 25 '22 07:10

aioobe