Solved

Synchonization Problem in my displaying thread and computing thread

Posted on 2002-04-17
4
165 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
Comment Utility
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
Comment Utility
What do you mean?
Shouldn't I have two instances as they are used in different threads?
0
 

Author Comment

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

Expert Comment

by:objects
Comment Utility
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

771 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

7 Experts available now in Live!

Get 1:1 Help Now