Solved

Pausing and restarting threads

Posted on 2004-09-12
5
202 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This video teaches viewers about errors in exception handling.

751 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