title: Android ThreadPool and BlockingQueue problems
body: <p>so I have created a singleton of threadpool:</p>
  
  <p>threadpool.java</p>
  
  <pre><code>package utils;
  
  import java.util.concurrent.BlockingQueue;
  import java.util.concurrent.LinkedBlockingQueue;
  import java.util.concurrent.ThreadPoolExecutor;
  import java.util.concurrent.TimeUnit;
  
  import android.annotation.SuppressLint;
  import android.os.Handler;
  import android.os.Message;
  import android.util.Log;
  
  public class TaskManager {
  
      // Sets the amount of time an idle thread will wait for a task before
  // terminating
  private static final int KEEP_ALIVE_TIME = 30;
  
  // Sets the Time Unit to seconds
  private static final TimeUnit KEEP_ALIVE_TIME_UNIT;
  
  private static final int CORE_POOL_SIZE = 2;
  
  private static final int MAXIMUM_POOL_SIZE = 2;
  
  // A queue of Runnable for the image download pool
  private final BlockingQueue&lt;Runnable&gt; mTaskQueue;
  
  // A managed pool of background download threads
  private final ThreadPoolExecutor mTaskThreadPool;
  
  private static TaskManager sInstance = null;
  
  private Handler mHandler;
  
  // A static block that sets class fields
  
  static {
      // The time unit for "keep alive" is in seconds
      KEEP_ALIVE_TIME_UNIT = TimeUnit.SECONDS;
  
      // Creates a single static instance of PhotoManager
      if (sInstance == null) {
          sInstance = new TaskManager();
          Log.d("init", "new TaskManager");
      }
  
  }
  
  public Handler getHandler() {
      return mHandler;
  }
  
  @SuppressLint("HandlerLeak")
  private TaskManager() {
      mTaskQueue = new LinkedBlockingQueue&lt;Runnable&gt;();
  
      mTaskThreadPool = new ThreadPoolExecutor(CORE_POOL_SIZE,
              MAXIMUM_POOL_SIZE, KEEP_ALIVE_TIME, KEEP_ALIVE_TIME_UNIT,
              mTaskQueue);
  
      mHandler = new Handler() {
          public void handleMessage(Message m) {
              switch (m.what) {
              case 0:
                  Log.d("start", (String) m.obj);
                  break;
              case 1:
                  Log.d("finish", (String) m.obj);
                  break;
              }
          }
      };
  }
  
  public static TaskManager getInstance() {
      return sInstance;
  }
  
  public void addTask(Runnable task) {
      sInstance.mTaskQueue.offer(task);
      Log.d("size after add", Integer.toString(sInstance.mTaskQueue.size()));
  }
  
  public void startTask() {
      if (!sInstance.mTaskQueue.isEmpty()) {
          sInstance.mTaskThreadPool.execute(sInstance.mTaskQueue.poll());
          Log.d("size after start", Integer.toString(sInstance.mTaskQueue.size()));
      } else
          Log.d("queue empty", "yes");
          return;
  }
  }
  </code></pre>
  
  <p>in another class I have a button,</p>
  
  <p>let's say everytime I click that <strong>button</strong>, it will call in order:</p>
  
  <ul>
  <li><code>TaskManager.getinstance().addTask(task)</code></li>
  <li><code>TaskManager.getinstance().startTask();</code></li>
  </ul>
  
  <p>so I click this button once, and everything seems normal, </p>
  
  <p>but if I click this once more after all tasks are done,</p>
  
  <p><code>Log.d("queue empty", "yes");</code> will be executed,</p>
  
  <p>my question is, I don't really understand what happens to the <strong>BlockingQueue</strong> inside this singleton, can anyone explain? thank you </p>