title: java program a infinite loop in thread cause hundred percent cpu
body: <p>I am quite new to Threads in Java, I am using an API which is using thread internally and listening data from the counter party, I am putting this data in an queue for further processing. I have created another Thread which is continuously reading this queue for retrieving data and processing and to write the results into text file. I am using while(true) statement to run infinite loop in thread this cause a hundred per cent of CPU usage and if I use sleep(10) in it add up latency which keep on increasing with time as I am receiving about 20 data item in one second.</p>
  
  <pre><code>public void run() { 
      while(true) { 
        try { Thread.sleep(10); } 
        catch (InterruptedException e2) { // TODO Auto-generated catch block 
          e2.printStackTrace(); 
       } 
       if (!(queue.isEmpt())) { 
          Tick quote=queue.take(); 
          processTuple(quote); 
       } 
      } // end while(true) 
    } // end run()
  </code></pre>
  
  <p>Could anyone suggest me solution where I can reduce CPU usage without adding latency.</p>