title: Pause and resume threads that are sleeping Java
body: <p>I have a thread that is running a simulation and is iterating a step of a dynamic system in the logic layer and then repainting the GUI to see the results in the screen. In order to let the user view how the system is changing there is a sleep between each repaint, but I also need to pause the simulation to observe changes, this is stop the repainting and the iteration of the system until the user resume the simulation.</p>
  
  <p>Currently looks like this:</p>
  
  <pre><code>simulationThread = new Thread(){
          @Override
          public void run(){
              for(int i=0; i&lt;iterations; i++){
                  world.iterate();
                  try{
                    Thread.sleep(100);
                  }catch( Exception e ){ 
                     e.printStackTrace();
                  }
                  view.repaint();
              }
          }
      };
  
      simulationThread.start();
  </code></pre>
  
  <p>How can I pause simulationThread and be able to resume it again?, I've tried to use wait() method, but there is problem when the thread is slept and wait is called. I suspect that I should change the way of how the thread wait until the next repaint in order to acomplish the pause/resume task but I'm not sure how.</p>