Solved

Synchonization Problem in my displaying thread and computing thread

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Apps blocked by Java 9 88
nextBoolean(double p) for Random class 3 41
login jsp example 24 63
running on tomcat not jboss eap 7.0 3 18
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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 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…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

839 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