Solved

Pausing and restarting threads

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

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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…
Suggested Courses

628 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