Take the 2-minute tour ×

I am getting following exception while staring a ScheduledThreadPoolExecutor. I use this to poll server every one minute.

01-11 18:45:50.243: E/AndroidRuntime(2427): Caused by: java.util.concurrent.RejectedExecutionException: pool=0/2147483647, queue=0

My code

stpe.scheduleWithFixedDelay(new Runnable() {  
        public void run() {  

            //Start Polling:
            Calendar cal = Calendar.getInstance();
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
            System.out.println( sdf.format(cal.getTime()) );
            Log.d("Polling", sdf.format(cal.getTime()) + " Polling for devices");

            HttpUtil httpUtil = new HttpUtil(mContext);

    },0, 60,TimeUnit.SECONDS);          

This is called when an activity is created. I am getting this exception every alternate time I start the activity i.e it works the next time it loads after crashing.

I shut it down using stpe.shutdownNow() before finishing the activity. But when next time this activity is created after loading the application I get the exception.

share|improve this question
add comment

1 Answer

You can't submit a new task once the executor is shutdown.You could store the future returned by the scheduleAtFixedRate method and cancel it before leaving the activity while keeping the executor running. Reschedule the task when the activity is resumed.

Or alternatively: shutdown the executor when leaving the activity and create a new one when the activity is resumed.

share|improve this answer
I am not creating a new task after shutting down the stpe. It is shutdown only while coming out the app. But when the next time the app loads and I try to submit I get this. –  suhas_sm Nov 15 '12 at 8:00
I am not an Android specialist but I think your app retains its state when you leave it and come back. So unless you create a new instance of the executor in onStart you will be using the previous instance that is shutdown. –  assylias Nov 15 '12 at 8:29
So you would create the executor in onCreate, submit the task in onStart, cancel it in onStop and shutdown the executor in onDestroy I think. –  assylias Nov 15 '12 at 8:40
add comment

Your Answer


