title: Why Thread.sleep is bad to use
body: <p>Apologies for this repeated question but I haven't found any satisfactory answers yet. Most of the question had their own specific use case:<br>
  <a href="https://stackoverflow.com/questions/9143719/java-alternative-to-thread-sleep">Java - alternative to thread.sleep</a><br>
  <a href="https://stackoverflow.com/questions/8182223/is-there-any-better-or-alternative-way-to-skip-avoid-using-thread-sleep1000-in">Is there any better or alternative way to skip/avoid using Thread.sleep(1000) in Java?</a></p>
  
  <p>My question is for the very generic use case. Wait for a condition to complete. Do some operation. Check for a condition. If the condition is not true, wait for some time and again do the same operation.</p>
  
  <p>For e.g. Consider a method that creates a DynamoDB table by calling its createAPI table. DynamoDB table takes some time to become active so that method would call its DescribeTable API to poll for status at regular intervals until some time(let's say 5 mins - deviation due to thread scheduling is acceptable). Returns true if the table becomes active in 5 mins else throws exception.</p>
  
  <p>Here is pseudo code:</p>
  
  <pre><code>public void createDynamoDBTable(String name) {
    //call create table API to initiate table creation
  
    //wait for table to become active
    long endTime = System.currentTimeMillis() + MAX_WAIT_TIME_FOR_TABLE_CREATE;
  
    while(System.currentTimeMillis() &lt; endTime) {
      boolean status =  //call DescribeTable API to get status;
      if(status) {
           //status is now true, return
           return
      } else {
          try {
              Thread.sleep(10*1000);
          } catch(InterruptedException e) {
          }
      }
    }
  
    throw new RuntimeException("Table still not created");
  }
  </code></pre>
  
  <p>I understand that by using <code>Thread.sleep</code> blocks the current thread, thereby consuming resources. but in a fairly mid size application, is one thread a big concern?<br>
  I read somewhere that use <code>ScheduledThreadPoolExecutor</code> and do this status polling there. But again, we would have to initialize this pool with at least 1 thread where runnable method to do the polling would run.</p>
  
  <p>Any suggestions on why using <code>Thread.sleep</code> is said to be such a bad idea and what are the alternative options for achieving same as above.</p>
  
  <p><a href="http://msmvps.com/blogs/peterritchie/archive/2007/04/26/thread-sleep-is-a-sign-of-a-poorly-designed-program.aspx" rel="noreferrer">http://msmvps.com/blogs/peterritchie/archive/2007/04/26/thread-sleep-is-a-sign-of-a-poorly-designed-program.aspx</a></p>