title: Thread run with a while loop
body: <p>I have a simple multi-threaded program as follows:</p>
  
  <p>I have T1,T2,T3...Tn threads, which all run in parallel.
  Each thread has a queue of objects, which I want to add/remove items from.
  In every single addition, I'd like to print it to the screen.</p>
  
  <pre><code>public void run() {
      while(!queue.isEmpty()) {
              Object obj = queue.poll();
              System.out.println(obj.toString());
      }
  }
  </code></pre>
  
  <p>And for addition, I simply use:</p>
  
  <pre><code>void addObj(Object obj) {
      this.queue.add(obj);
  }
  </code></pre>
  
  <p>In the same class of the thread.</p>
  
  <p>I am using <code>addObj</code> across the program, in different times.</p>
  
  <p><strong>The problem</strong> when I add objects to the queue, it doesn't seem that <code>run</code> even cares.
  In some cases it did print the new object, but in many cases it did not, and it still considered the queue as empty.</p>
  
  <p>How can I make sure that the thread is aware of this item being added?
  Should I use any callback?
  What is the right approach for such tasks?</p>
  
  <ul>
  <li>I also noticed that when I use <code>Thred.sleep()</code> it helps, but it looks pretty ugly, I guess. </li>
  </ul>
  
  <p><strong>UPDATE</strong>: each thread <strong>should</strong> have it's own thread. I am creating the threads in a separte "thread-manger" which starts them one by one.
  Each thread has it's own private queue, which should be filled on the fly.
  Items can be add/removed to the queues in any given time, before and after the threads started.</p>