Solved

Synchonization Problem in my displaying thread and computing thread

Posted on 2002-04-17
4
166 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
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 “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

910 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

23 Experts available now in Live!

Get 1:1 Help Now