1

I have the below condition, Can someone please help:

if(checkcondition){ --------(A)
...
some code
...
}
else{
sleep for 20 sec
go back to checkconfdition loop @ (A)
}

Any help is appriciated in advance.

Thanks Vaibhav

jmj
  • 237,923
  • 42
  • 401
  • 438
vaibhav
  • 3,929
  • 8
  • 45
  • 81
  • 2
    What is your problem? Define problem. Or do you want someone to write code for this scenario? – Umesh K Feb 23 '11 at 09:57
  • i need to code this situtation, i am a novice to threading thats why i am askin what should be the approach, Thanks – vaibhav Feb 23 '11 at 09:58
  • 1
    if you are a novice to threading, the first thing you should ask is "Do I really need threading?". I believe it's also what experts ask themselves ... – Mitch Wheat Feb 23 '11 at 10:00
  • i know a bad question but i need to ask as i have some code already writtern which uses threading and i need to make sure that unless this condition gets satisfied the thread waits for the intervals of 20 secs. – vaibhav Feb 23 '11 at 10:02

2 Answers2

3
while(true){
    if (checkcondition1) {
        //some code
    } else {
        try {
           Thread.sleep(20*1000);
        } catch (Exception ex) {
        //some action
    }
}
NguyenDat
  • 4,129
  • 3
  • 46
  • 46
jmj
  • 237,923
  • 42
  • 401
  • 438
  • after Thread.sleep would the thread go back to the checkCondition and check for the condition again? – vaibhav Feb 23 '11 at 10:00
  • 2
    @vaibhav: Yes, because of the "while(true)" loop. However, you shouldn't catch Exception here, just InterruptedException. – Jon Skeet Feb 23 '11 at 10:02
  • my inference is that while(true) would keep it running always. – vaibhav Feb 23 '11 at 10:03
  • thanks for the help jigar how about this? code while(Somecondition==false || System.currentTimeMillis()-startTime== 900000) { wait(20000); } notify(); – vaibhav Feb 23 '11 at 10:20
  • this is some custom condition its like either the condition turns to be true or we ve been retrying since 15 mins. – vaibhav Feb 23 '11 at 10:31
  • see its like either somecondition is true or the difference between current time and the variable startTime is more then 15 mins so that is like while(somecondition || (System.currentTimeMillis()-startTime)< 900000) wait(20000); log.debug("Waiting for old execution to finish"); } notify(); would this work fine? – vaibhav Feb 23 '11 at 10:36
  • it should now with changed condition. – jmj Feb 23 '11 at 10:41
  • one last question Jigar should i call the notify() at all? – vaibhav Feb 23 '11 at 10:53
  • @JigarJoshi Timing using Thread.sleep is flawed http://stackoverflow.com/questions/1937619/how-can-i-measure-time-in-java-not-susceptible-to-system-clock-changes#comment-12202854 – Pacerier Mar 08 '12 at 11:53
3

Sleeping is not the best way to get a thread to wake up and do stuff. You should use call wait to make the thread wait, and then your other thread that sets the checkcondition would also call notify to wake the first thread up.

The advantage of doing this is that the thread will wake up straight away, instead of having to wait up to 20 seconds for it to realise there's work to do.

Search for Java wait notify and you'll find plenty of examples. Related Stack Overflow question: A simple scenario using wait() and notify() in java.

Community
  • 1
  • 1
Nick
  • 11,475
  • 1
  • 36
  • 47
  • Also note one of the answers in that linked answer, saying that the newish concurrency classes such as Semaphore in Java are preferable to manually doing `wait` and `notify`. Either are preferable to using `sleep` though. – Nick Feb 23 '11 at 10:08
  • Another example - if your condition is "mylist.isEmpty()" and your action is taking an item from it then you could look at replacing your list with a `LinkedBlockingQueue`, then call `mylist.take()` in your loop, which will block until an item is available. – Nick Mar 01 '11 at 13:47
  • You said that Thread.sleep is not guaranteed to come back, that I understand. But isn't Object.wait not guaranteed to come back as well? – Pacerier Mar 08 '12 at 11:50
  • I don't think I said that `sleep` is not guaranteed to return - in fact it _will_ always return eventually, while `wait` might never return if you neglect to call `notify`. – Nick Mar 08 '12 at 15:32
  • Ok just answered my own question lol. The doc http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#sleep(long) says Thread.sleep's "timing is subject to the precision and accuracy of system timers and schedulers", which means it will never return, however Object.notify is guaranteed to wake a Thread that waits on that object http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#wait(long). – Pacerier Mar 09 '12 at 02:15
  • Don't know how you inferred that fact from that text :) The bit you quoted just means that depending on the computer, it won't be _exactly_ 20 seconds. It'll still return after that. You're correct about `notify` though - that is guaranteed to wake up a waiting thread. – Nick Mar 09 '12 at 09:29