title: asynchronous threads each running an infinite loop
body: <p>I'm implementing a program which contains different tasks and all have implemented Runnable. e.g. there is a task which works on a database and sends some of the tuples to a synchronized shared memory and subsequently, there is another thread which checks the shared memory and sends messages to a queue. Moreover, these two threads iterate over an infinite while loop. </p>
  
  <p>Already, I have used the fixedThreadPool to execute these threads.  </p>
  
  <p>The problem is that sometimes program control remained in the first running thread and the second one never gets the chance to go to its running state. </p>
  
  <p>Here is a similar sample code to mine: </p>
  
  <pre><code>public class A implements Runnable {
      @Override
      public void run() {
          while(true) {
          //do something
          }
      }
  }
  public class B implements Runnable {
      @Override
      public void run() {
          while(true) {
          //do something
          }
      }
  }
  public class Driver {
      public static void main(String[] args) {
          ExecutorService executorService = Executors.newFixedThreadPool(2);
          A a = new A();
          executorService.execute(a);
          B b = new B();
          executorService.execute(b);
      }
  }
  </code></pre>
  
  <p>I'd also done something tricky, make the first thread to sleep once for a second after a short period of running. As a result, it makes the second thread to find the chance for running. But is there any well-formed solution to this problem? where is the problem in your opinion?</p>