Solved

Pausing and restarting threads

Posted on 2004-09-12
5
198 Views
Last Modified: 2010-03-31
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
Comment
Question by:agrees
5 Comments
 
LVL 3

Expert Comment

by:CI-Ia0s
ID: 12039712
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
 
LVL 2

Expert Comment

by:SaMuEl
ID: 12040081
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
 
LVL 92

Accepted Solution

by:
objects earned 125 total points
ID: 12040154
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
 

Author Comment

by:agrees
ID: 12042378
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
 
LVL 92

Expert Comment

by:objects
ID: 12042930
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
login jsp example 24 85
configure dependency in POM for new database 3 45
servlet and mdb, jms error 1 57
junit as external jar or library 7 38
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question