title: Communicating between two threads
body: <p>I have a thread, <code>A</code> which has a list. List holds some objects. Now I want to design a mechanisim by which I can send some message to thread <code>A</code>.</p>
  
  <p><code>Thread A</code> runs in a loop (it does not wait or sleep).
  Some other thread, <code>B</code>, sends some message to thread <code>A</code> and thread <code>A</code> empties all its queues.</p>
  
  <p>How can I send messages between threads?</p>
  
  <pre><code>class A extends Thread {
      List&lt;Object&gt; objs = something; //Init it
      void run() {
          while(true) {
              //Body which works on objects.
              //After receiving an external message, "A" should perform some action, for example, empty objects.
          }
      }
  }
  </code></pre>
  
  <p>EDIT:
  Can I do it like this?</p>
  
  <pre><code>class A extends Thread {
      List&lt;Object&gt; objs = something; //Init it
      Boolean flag = false;
  
      public void setFlag(boolean value) {
          synchronized(flag) {
              this.flag = value;
          }
      }
  
      public void getFlag() {
          synchronized(flag) {
              return this.flag;
          }
      }
  
      void run() {
          while(true) {
              //Body which works on objects.
              //After receiving an external message, A should perform some action, for example, empty objects.
              if (getFlag == true)
                  //Empty list
          }
      }
  }
  </code></pre>