Results 1 to 10 of 10
Thread: Waiting on a condition
- 04-21-2011, 09:42 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 15
- Rep Power
- 0
- 04-21-2011, 11:02 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
A loop requires the condition to be true already to enter the loop body. So if variable x is originally false, using it in the loop condition will make it so you never enter the loop. You can negate the condition to allow the loop to run until the condition is true.
Java Code:boolean x = false; while(x){//loop doesn't get entered since the condition is always false} while(!x){//enters the loop as long as x is false, when x changes to true it will leave the loop}
- 04-22-2011, 12:14 AM #3
Member
- Join Date
- Apr 2011
- Posts
- 15
- Rep Power
- 0
Thanks sunde :) I was wondering is there is a way to wait on a condition without using an empty loop?
- 04-22-2011, 12:18 AM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Well if you have an empty loop statement you will create an finite loop, it will just enter the loop, do nothing and then repeat the loop. Each time through not doing anything, thus the condition will always be what it originally was.
Can you give more details as to what you want to do?
- 04-22-2011, 12:30 AM #5
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
It depends on exactly what you want to achieve, but in general just using an empty loop is not a good idea because the processor is occupied racing round the loop and has no time to handle anything else.
It is generally better to use a loop with a sleep for a certain time, to free up processor time for other threads. See Pausing Execution with Sleep.
-
Another way is to use an observer/observable and have the observe react when the observable notifies it of an event. Most GUI libraries (that I'm familiar with) are based on this paradigm.
- 04-22-2011, 09:34 AM #7
Member
- Join Date
- Apr 2011
- Posts
- 15
- Rep Power
- 0
Thanks guys, the observer pattern seems mostly what I need. :) Could anyone give me an example of it please? Lets say my code is:
Java Code:class Class1{ boolean isTrue = false; public boolean getIsTrue(){ return isTrue; } }How would I change this to become observable please?Java Code:class Class2{ while(Class1.getIsTrue()){ //wait } //code to be executed after wait }
- 04-22-2011, 09:49 AM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
A Class1 object is the observable and Class2 objects are the observers. Observers should register itself at the observable and the observable should notify the observers when its state changes. Read the API documentation for the Observable/Observer classes/interfaces. If you're talking multiple threads your original wait() paradigm is also fine: as long as a condition is not met your object should wait(). If the condition changes somewhere the object responsible for the condition change should notifyAll() objects waiting on that condition. Read the API documentation for the Thread and Object classes. For a more solid approach read all classes APIs in the java.util.concurrent package.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-22-2011, 10:01 AM #9
Member
- Join Date
- Apr 2011
- Posts
- 15
- Rep Power
- 0
Thanks Jos!
- 04-22-2011, 10:05 AM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
You're welcome of course; if you go for a single threaded approach also read the API documentation of the PropertyChangeSupport; it takes away the nitty gritty details of managing the observers and manages the groups of properties for which the observers should be notified. Also there are numerous Listeners for certain events, maybe you can use one of those.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Using Thread waiting() method
By nicoeschpiko in forum New To JavaReplies: 7Last Post: 12-11-2010, 08:24 PM -
Waiting for a carriage return
By Ebodee in forum New To JavaReplies: 1Last Post: 02-12-2010, 03:46 AM -
Thread RUNNABLE or WAITING
By Pushkar in forum Threads and SynchronizationReplies: 10Last Post: 01-14-2010, 01:36 AM -
Once again: waiting in a thread loop.
By willemjav in forum Threads and SynchronizationReplies: 115Last Post: 09-22-2008, 01:35 PM -
waiting for a file
By Fleur in forum New To JavaReplies: 2Last Post: 06-23-2008, 08:18 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks