Results 1 to 5 of 5
Thread: How to create a block method?
- 12-22-2011, 08:09 AM #1
How to create a block method?
I want to implement something that looks like this:
How do I make the waitForSignal() method? I want that method to block the current thread until another method calls a certain method to give it the signal (namely, a button being pressed in the GUI app)Java Code:while(true){ waitForSignal(); //do the code }
Is there a good up-to-date book/tutorial you'd recommend for getting good control of java concurrency?
- 12-23-2011, 06:01 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
Re: How to create a block method?
Are you familiar with the wait/notify methods? Guarded Blocks (The Java™ Tutorials > Essential Classes > Concurrency)
- 12-26-2011, 06:58 AM #3
Re: How to create a block method?
^ I came across that page while doing the search on my own before posting, it kept failing for me and giving me an IllegalMonitorStateException, silly me that was because I was calling the notify() method that belonged to the anonymous ActionListener class attached to the button and not the main class of the program on which I invoke the wait() method...
Thanks for your help...:)
- 12-26-2011, 10:52 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
Re: How to create a block method?
A wait or notifty(All) call on an object only works if the caller holds the 'monitor' for that object; so:
kind regards,Java Code:// in one thread: synchronized(anObject) { // get the monitor of anObject while (!aCondition()) anObject.wait(); // wait on anObject } // while in another thread: synchronize(anObject) { // get the monitor on the same 'anObject' // make aCondition return true anObject.notifyAll(); // wake up all waiting threads }
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-26-2011, 05:18 PM #5
Re: How to create a block method?
^ Yeah I know, my problem was that I wrote it like this:
I didn't notice that I was calling the notifyAll() method that belonged to the ActionListener object and not the containing object on which I call the wait() method...Java Code:myButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ startExecution = true; notifyAll(); } }
Similar Threads
-
calling yield() method in synchronized block
By Ash-infinity in forum New To JavaReplies: 2Last Post: 12-04-2012, 05:35 PM -
Help. Create 2D array method that returns index of row that contains the most zeros.
By jlss4e in forum New To JavaReplies: 3Last Post: 08-21-2011, 02:25 AM -
Why can't a static method create inner class objects
By Humbly in forum New To JavaReplies: 6Last Post: 02-12-2011, 06:56 AM -
When I create method...
By isuru in forum AWT / SwingReplies: 6Last Post: 05-13-2010, 09:42 PM -
Modify/create AVI's INFO BLOCK
By Agus211 in forum New To JavaReplies: 0Last Post: 02-11-2008, 03:20 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks