Results 1 to 6 of 6
Thread: Creating Blocking Methods
- 06-11-2009, 09:44 AM #1
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
Creating Blocking Methods
How do you write a method that blocks until a condition is fulfilled to continue, other than using a while loop to wait for the condition.
e.g.
I've been googling it, but I haven't been able to find any good articles. Any links/hints/old code is appreciated.Java Code:public void myBlockingMethod(){ when(condition.isFullfilled()); }
Thanks,
Singing BoyoIf the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
- 06-11-2009, 09:48 AM #2
Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 5
As soon as you full fill the condition, use:Java Code:public final Object waitObj = new Object(); public void myBlockingMethod() { synchronized(waitObj) { while (!condition.isFullfilled()) waitObj.wait(); // throws exception, cba to add it here } }
Java Code:synchronized(waitObj) { waitObj.notifyAll(); }
~MattI die a little on the inside...
Every time I get shot.
- 06-11-2009, 09:49 AM #3
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
wait and notify
See Lesson: Concurrency (The Java™ Tutorials > Essential Classes)
Edit: A bit slow.
- 06-11-2009, 09:52 AM #4
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
Hmm... was hoping to avoid creating numerous threads, though it may be unavoidable.
If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
- 06-11-2009, 09:55 AM #5
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
I'm sorry, but how would this "condition be fulfilled" if another thread did not fulfill it? Or are you trying to "wait" until an FTP is finished, or something like that?
- 06-11-2009, 10:44 AM #6
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
It's a server/client program. I'm trying to create an extension of ServerSocket that allows threads to request sockets from a specified InetAddress, so that they can retrieve new connections by their client. (The Server "talks" to the client through multiple Sockets)
If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
Similar Threads
-
Trouble with static methods and boolean equals() methods with classes
By dreamingofgreen in forum New To JavaReplies: 8Last Post: 04-16-2012, 11:00 PM -
How to get methods to see variables in other methods
By ejs7597 in forum New To JavaReplies: 4Last Post: 04-03-2009, 06:36 AM -
[SOLVED] Blocking Queues - how?
By sebo in forum New To JavaReplies: 4Last Post: 12-08-2008, 12:12 PM -
[SOLVED] Site Blocking
By Mir in forum NetworkingReplies: 12Last Post: 07-03-2008, 06:04 AM -
Non Blocking Network
By mathias in forum NetworkingReplies: 1Last Post: 08-07-2007, 06:49 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks