• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help on synchronization...

 
R Ludington
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a boolean method that calls another method and needs to wait until it is finished. I put a sleep into the first method code but when it sleeps everything sleeps .. so the entire program hangs. How can I fix this ? Is this a thread problem ?
Here is simulation and the calling / wait code:

boolean mthod
{
code
code
code

JCheckBx b = new JCheckBx(x); // call to method that puts up a frame
// that contains several checkboxes.
// sets i to 1 when finished.
// results stored in prodlist.
i = 0;
while (i == 0)
{
i = b.getdone(); // check to see if done

boxwait(10000); // call to a wait method
// this can be a sleep statement..
// sleep does not work here either.

}
StringBuffer newlst = b.getprodlist(); get the result
return true;
} // end of boolean method
 
Yaroslav Chinskiy
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

It's not clear what you are trying to do.

It look like you execute the code on the same thread which means calling sleep() will block everything.


What do you need to wait for?

Usually producer/consumer type of problem, you can use wait/notify.

Chapter 3 has some good point about it.
http://java.sun.com/docs/books/effective/chapters.html

Since you need to get condition from b.getprodlist(), you can have some kind wait/notify in there. The method will block until the conition is set but will not hang the whole application.
 
R Ludington
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my current code trys to use the wait.. here it is:

My JcheckBx method does this :

start
put up a frame with check boxes

listen for checks and modify prodlist

listen for next button //actual code follows

ActionListener nextbutton = new ActionListener()
{
public synchronized void actionPerformed(ActionEvent e)
{
// pushed the Next button
done = 1;
JCheckBx.this.setVisible(false);
notifyAll();
}
};
nextButton.addActionListener(nextbutton);

listen for quit button
modify done
notifyAll();
end


and the wait code method is as follows:

public synchronized void boxwait(int c)
{
try
{
wait(c);
}
catch (InterruptedException e) { }

}

So if you put it togeather from my first post .. can you see what I'm trying to do ?
Am I not using the wait/notify combination correctly ? This combination makes everything sleep.
 
Yaroslav Chinskiy
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dont think you use wait/notify the right way.


When you use synchronized method it is the same as synch on this object.

So you notify threads that wait on the ActionListener, but wait on the object that implements boxwait()

Also making actionPerformed synchronized may effect Swing's event dispatcher thread.

The swing gui runs on one thread, looks like you put the dispatcher thread on wait and that make the whole GUI freeze.

Here is example of wait/notify in a queue:


Also if you just create the frame to get some input from the user, why dont you use dialog box or option pane?
[ July 14, 2004: Message edited by: Yaroslav Chinskiy ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!