Solved

Pausing and restarting threads

Posted on 2004-09-12
5
165 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
count8 challlenge 13 85
ArrayLists in ArrayLists/HashMaps? 4 40
Understanding websocket example in spring 1 42
word0 challenge 3 58
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to implement Singleton Design Pattern in Java.

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now