Results 1 to 5 of 5
Thread: How to create a block method?
- 12-22-2011, 09:09 AM #1
How to create a block method?
I want to implement something that looks like this:
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, 07:01 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,638
- Rep Power
- 13
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, 07: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, 11:52 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
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:
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 }
JosBuild a wall around Donald Trump; I'll pay for it.
- 12-26-2011, 06:18 PM #5
Re: How to create a block method?
^ Yeah I know, my problem was that I wrote it like this:
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, 06: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, 03:25 AM -
Why can't a static method create inner class objects
By Humbly in forum New To JavaReplies: 6Last Post: 02-12-2011, 07:56 AM -
When I create method...
By isuru in forum AWT / SwingReplies: 6Last Post: 05-13-2010, 10:42 PM -
Modify/create AVI's INFO BLOCK
By Agus211 in forum New To JavaReplies: 0Last Post: 02-11-2008, 04:20 PM
Bookmarks