Solved

Synchonization Problem in my displaying thread and computing thread

Posted on 2002-04-17
4
170 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
[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
  • 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

Technology Partners: 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

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

707 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