Results 1 to 10 of 10
  1. #1
    nephos is offline Member
    Join Date
    Apr 2011
    Posts
    15
    Rep Power
    0

    Default Waiting on a condition

    What is the preferred way of waiting until a condition becomes true? Right now I have something like:

    Java Code:
    while(isTrue()){//wait}
    Is there a better way of doing this please? Thanks!

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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}

  3. #3
    nephos is offline Member
    Join Date
    Apr 2011
    Posts
    15
    Rep Power
    0

    Default

    Thanks sunde :) I was wondering is there is a way to wait on a condition without using an empty loop?

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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?

  5. #5
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    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.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  7. #7
    nephos is offline Member
    Join Date
    Apr 2011
    Posts
    15
    Rep Power
    0

    Default

    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;
    }
    }
    Java Code:
    class Class2{
    
    
    while(Class1.getIsTrue()){
    //wait
    }
    
    //code to be executed after wait
    }
    How would I change this to become observable please?

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by nephos View Post
    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;
    }
    }
    Java Code:
    class Class2{
    
    
    while(Class1.getIsTrue()){
    //wait
    }
    
    //code to be executed after wait
    }
    How would I change this to become observable please?
    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    nephos is offline Member
    Join Date
    Apr 2011
    Posts
    15
    Rep Power
    0

    Default

    Thanks Jos!

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by nephos View Post
    Thanks Jos!
    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Using Thread waiting() method
    By nicoeschpiko in forum New To Java
    Replies: 7
    Last Post: 12-11-2010, 08:24 PM
  2. Waiting for a carriage return
    By Ebodee in forum New To Java
    Replies: 1
    Last Post: 02-12-2010, 03:46 AM
  3. Thread RUNNABLE or WAITING
    By Pushkar in forum Threads and Synchronization
    Replies: 10
    Last Post: 01-14-2010, 01:36 AM
  4. Once again: waiting in a thread loop.
    By willemjav in forum Threads and Synchronization
    Replies: 115
    Last Post: 09-22-2008, 01:35 PM
  5. waiting for a file
    By Fleur in forum New To Java
    Replies: 2
    Last Post: 06-23-2008, 08:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •