Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 228
  • Last Modified:

Pausing and restarting threads

Hi all,

I have a thread which consists of a loop which runs a simulation.  Because this simulation can take a long time and is very processor intensive, I want to allow the user to pause it.  

I'm not sure the best way to do this though.  Here's my latest effort.  It is obviously no good because the infinite while loop when paused takes up loads of processor time!   But it should hopefully show what I'm trying to do.

  boolean isPaused = false;
 
  public void run() {
    int currentIteration = 0;
    int simulationLength= 500000;
    while (currentIteration < simulationLength) {
      if(!isPaused) {
        doABigCalculation();
      }
      currentIteration ++
     }
   }
    public void actionPerformed(ActionEvent e) {
    if (e.getSource() == pauseButton) {
      if (isPaused == true) {
        isPaused = false;
        pauseButton.setIcon(pauseIcon);
      }
      else {
        isPaused = true;
        pauseButton.setIcon(goIcon);
      }
    }

 
0
agrees
Asked:
agrees
1 Solution
 
CI-Ia0sCommented:
I'd suggest breaking it into a series of methods. At the end of each method, your program would check if the user had paused the thread. If the user had paused, it would stop and record what method it had stopped on. If the user hadn't paused, it would execute the next method in the sequence. When the user unpaused, it would check what method it had been on and would pick up there.
0
 
SaMuElCommented:
class threadName extends Thread{
boolean isPaused = false;
 
  public void run() {
    int currentIteration = 0;
    int simulationLength= 500000;
    while (currentIteration < simulationLength) {
      if(!isPaused) {
        doABigCalculation();

      }
      currentIteration ++
     }
   }
}

    public void actionPerformed(ActionEvent e) {
    if (e.getSource() == pauseButton) {
      if (isPaused == true) {
        isPaused = false;

threadName.notifyAll();
        pauseButton.setIcon(pauseIcon);
      }
      else {
        isPaused = true;
threadName.wait();

        pauseButton.setIcon(goIcon);
      }
    }

Try something along these lines, using the wait and notifyAll methods in your action performed method.
0
 
objectsCommented:
try something like:


  boolean isPaused = false;
 
  public void run() {
    int currentIteration = 0;
    int simulationLength= 500000;
    while (currentIteration < simulationLength) {
      while(isPaused) {
        synchronized (this) {
          wait();
        }
      }
        doABigCalculation();
      currentIteration ++
     }
   }
    public void actionPerformed(ActionEvent e) {
    if (e.getSource() == pauseButton) {
      if (isPaused == true) {
        isPaused = false;
        pauseButton.setIcon(pauseIcon);
        synchronized (this) {
          notify();
        }
      }
      else {
        isPaused = true;
        pauseButton.setIcon(goIcon);
      }
    }
0
 
agreesAuthor Commented:
That works well, thanks objects.  Now I just have to work out *why* it works! :)

SaMuEl  >  I tried what you suggested but it was throwing exceptions due to the thread that handles the button press being different to the thread which runs the simulation and I couldn't work out a way round this.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now