appleb
asked on
Synchonization Problem in my displaying thread and computing thread
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 .addSoluti on(board_b lock);
solutionNum++;
System.out.println("soluti on 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????
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
solutionNum++;
System.out.println("soluti
}
**************************
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(
{
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????
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
oh, I see what you mean now. I will try that to see what happens. Thanks!
ASKER
Shouldn't I have two instances as they are used in different threads?