title: How to overcome Method calls Thread.sleep() with a lock held warning in findbugs?
body: <p>I have the following synchronized method which is used to shut down the task manager. The logic is to check for active tasks and if there is any, I will wait for 1 second and will check again.</p>
  
  <p>But it seems like find bugs plugin is not allowing me to use Thread.sleep() inside a synchronized block and giving the following warning</p>
  
  <blockquote>
    <p>Method calls Thread.sleep() with a lock held
    This method calls Thread.sleep() with a lock held. This may result in very poor performance and scalability, or a deadlock, since other threads may be waiting to acquire the lock. It is a much better idea to call wait() on the lock, which releases the lock and allows other threads to run.</p>
  </blockquote>
  
  <p>Any idea on how to overcome this issue ?</p>
  
  <pre><code>public synchronized void stop() {
  
      // try to wait a bit for task shutdown
      for (int i = 0; i &lt; 5; i++) {
          if (activeTaskCount == 0) {
              break;
          }
          try {
              Thread.sleep(1000);
          } catch (InterruptedException ignore) {
          }
      }
  
  
      if (activeTaskCount &gt; 0) {
          log.warn("Unable to shutdown all polling tasks of " + jmsConsumerName);
      }
  
      if (JMSTaskManagerState != STATE_FAILURE) {
          JMSTaskManagerState = STATE_STOPPED;
      }
      log.info("Task manager for jms consumer " + receiveTimeout + " shutdown");
  }
  </code></pre>