Solved

Synchonization Problem in my displaying thread and computing thread

Posted on 2002-04-17
4
167 Views
Last Modified: 2011-04-14
Hi, there is a problem when I try to run the following
codes. What I am trying to do is that when I run the
program in a "single-step" mode, it should execute the
addSolution function once each time, and then it should
wait until the user press the "Step" button.

The following code is from a thread doing the main
computation job. It is put into a loop, so that I am
sure if I run it without "Step" mode, it will be
executed at least 20 times based on my test file.

***************************************************************
stepExec = new StepSync();
...

if (numused == blockNum) {

      stepExec.step_run(); //I use this line to check whether it
                        // should pause or continue
     
      NewPuzzleFrame.solutionBar.addSolution(board_block);
      solutionNum++;
      System.out.println("solution Num = "+solutionNum);
}

***************************************************************
Here is the StepSync class. I use its functions to notify the
computation to continue or wait. It is somewhat like a
synchonization scheme.



***************************************************************
class StepSync {
public static boolean stepSignal = false;

public synchronized void step_run() {
  try {
    while (stepSignal == false) wait(); //pause
    stepSignal = false; //begin to run, reset the signal

    }catch (InterruptedException e) {}
}

public synchronized void step_cont() {
  try {
    if (stepSignal == false)
    {
      stepSignal = true;
      notifyAll();
    }
  } catch (Exception e) {}
}

} // end of StepSync Class

******************************************************************

In a seperate displaying thread, I have the following code to trap
a press on the "Step" button;

stepCont = new StepSync();
  JButton button3 = new JButton("Step");
    button3.addActionListener(new ActionListener()
    {
      public void actionPerformed (ActionEvent e)
      {
        stepCont.step_cont();
      }
    });
   
   
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
OK. THE PROBLEM IS:
I debug this, and find that after two or three times of correct wait-notified
transitions, the computation thread will enter a state that it can not
be waked up by the button pressing. So the program has no response since then.

Why????
0
Comment
Question by:appleb
  • 2
  • 2
4 Comments
 
LVL 92

Accepted Solution

by:
objects earned 50 total points
ID: 6949020
From you code it appears that their are two instances of your StepSync() class but this may just be for the sake of the example.
0
 

Author Comment

by:appleb
ID: 6949183
What do you mean?
Shouldn't I have two instances as they are used in different threads?
0
 

Author Comment

by:appleb
ID: 6949196
oh, I see what you mean now. I will try that to see what happens. Thanks!
0
 
LVL 92

Expert Comment

by:objects
ID: 6949234
0

Featured Post

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

777 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