title: Always run a processing thread if a message queue is not empty
body: <p>Whenever the server receives a message from a client, the message will be put in a queue. Multiple threads can input data in the queue, but only one thread should read/process from it at a time. As long as there are messages in the queue, the processing thread should be alive. How do I make this work?</p>
  
  <p>If I put the processing logic in a synchronized method, and start a new thread calling this method for every request, won't it be a waste of resources having all those threads in memory, waiting their turn?</p>
  
  <p>On the other hand, if I use a single processing thread, with each request trying to start it if it's not already running:</p>
  
  <pre><code>public void onMessage(String message) {
      addToQueue(message);
      synchronized (lock) {
          if (!processingThread.isAlive())
              processingThread.start();
      }
  }
  
  // processing thread
  public void process() {
      while (true) {
          String message = queue.poll();
          synchronized (lock) {
              if (message == null)
                  return;
          }
          // process message
      }
  }
  </code></pre>
  
  <p>How can I make sure I don't run into this scenario:</p>
  
  <ol>
  <li>Processing thread polls the queue for the next element. It gets the shared lock and enters a synchronized block. It checks and sees the element it read from the queue is <code>null</code>, so the queue is empty. <strong>The thread releases the lock, but its <code>isAlive</code> flag hasn't been set to false yet.</strong></li>
  <li>Client request arrives, adding a message in the queue. The receiving method gets the lock, and enters its synchronized block. The processing thread is still alive, so it doesn't need to be started. The method releases the lock.</li>
  <li>The processing thread has now released the lock and dies.</li>
  </ol>
  
  <p>In this case, there will be one unprocessed message in the queue. Can this scenario happen? As the bold sentence suggests, can the lock be released before the thread dies?</p>