title: Threading in Java REST web service
body: <p>I have a REST web service which is consuming too many CPU resources when the number of requests gets too high.</p>
  
  <p>This is believed to be caused by a while() loop in the response generation, which normally takes a few milliseconds to complete, however under some circumstances it can take a few seconds. </p>
  
  <p>The fix for this appears to be to use wait() and notify() according to <a href="http://publib.boulder.ibm.com/infocenter/javasdk/tools/index.jsp?topic=/com.ibm.java.doc.igaa/_1vg00013ae41264-1189dcf6fc0-7ffd_1001.html" rel="nofollow">this</a> however I don't understand why this will reduce CPU usage?</p>
  
  <p>Would this new thread be handled outside the web service, thereby freeing it up to handle more requests? Can someone please explain?</p>
  
  <p>Thanks!</p>
  
  <p>Jon.</p>
  
  <p>Edit:</p>
  
  <p>I may have found my own answer <a href="http://www.java-samples.com/showtutorial.php?tutorialid=306" rel="nofollow">here</a></p>
  
  <p>It seems that my code of <code>result = get()</code> constantly polls until there is a response, which consumes more CPU resources. By placing this in a thread, less resources are consumed.</p>
  
  <p>Is this a correct understanding?</p>