title: How to thread-safe signal threads to pause in Java
body: <p>I have a bunch of threads running concurrently. Sometimes a thread needs to notify other threads to wait for it to finish a job and signal them again to resume. Since I'm somehow new to Java's synchronization, I wonder what is the right way to do such thing. My code is something like this:</p>
  
  <pre><code>private void Concurrent() {
      if (shouldRun()) {
          // notify threads to pause and wait for them
          DoJob();
          // resume threads
      }
  
      // Normal job...
  }
  </code></pre>
  
  <h2>Update:</h2>
  
  <p>Note that the code I wrote is inside a class which will be executed by each thread. I don't have access to those threads or how they are running. I'm just inside threads.</p>
  
  <h2>Update 2:</h2>
  
  <p>My code is from a crawler class. The crawler class (crawler4j) knows how to handle concurrency. The only thing I need is to pause other crawlers before running a function and resume them afterwards. This code is the basics of my crawler:</p>
  
  <pre><code>   public class TestCrawler extends WebCrawler {
      private SingleThread()
      {
          //When this function is running, no other crawler should do anything
      }
  
      @Override
      public void visit(Page page) {
          if(SomeCriteria())
          {
              //make all other crawlers stop until I finish
              SingleThread();
              //let them resume
          }
  
          //Normal Stuff
      }
     }
  </code></pre>