Quang Nhat
Quang Nhat

Reputation: 155

ExecutorService can not reuse its thread

I have the following task:

void task()
{
  Looper.preapre();
  handler = new Handler();
  runnable = new Runnable()
  {
     if(notDone)
     {
        doSth();
        handler.postDelay(runnable,timeInterval);
     }
     else
     {
        handler.removeCallBacks(runnable);
        returnResult(); / the task is done and return some results
     }
  }
   handler.post(runnable);
   Looper.loop();
}

I use ExecutorService with single worker thread to run this task, the 1st time it runs just fine but the 2nd time the task is never be executed because the worker thread is still running the 1st task although i call removeCallBacks. Does anyone know where the problem is? Thanks in advance.

PS: I have try Looper.quit() but it kills the thread so the executor can not run the task on the dead thread.

Upvotes: 0

Views: 41

Answers (1)

Quang Nhat
Quang Nhat

Reputation: 155

After doing a research, i found that when a looper is attached to a thread, it will keep the thread active all the time even the thread is idle, so the ExecutorService can not get back the idle thread

Upvotes: 1

Related Questions