aspose file tools
The moose likes Threads and Synchronization and the fly likes how to set timer for wait() call Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "how to set timer for wait() call" Watch "how to set timer for wait() call" New topic
Author

how to set timer for wait() call

Vinney Shanmugam
Ranch Hand

Joined: Aug 27, 2008
Posts: 104
This is my code which waits for notification from some other thread. In case, if i dont want it to be indefinitely waiting for the notification, how can i give a timer value to keep waiting till that time and once that time is reached, come out of wait().

try {
wait();
} catch (InterruptedException e) {
}

I have tried wait(time) API. Can it be done without this using Timer class?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32712
    
    4
I always thought it was obj.wait(1000); for 1 second. Remember the timing depends on the system clock, and is not precise.

Sounds like a thread which would get better attention on the threads forum, so I shall move you.
Pawan Arora
Ranch Hand

Joined: Sep 14, 2008
Posts: 105
whenever you declare wait method in any method of an object, you have to declare it synchronized, otherwise an unchecked exception will be thrown.
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

vinod:I have tried wait(time) API. Can it be done without this using Timer class?


Yeah it can be done via the Timer class but i would not suggest it.
The reason being that you will start a new thread(if Timer is not already used) to keep a watch on the running thread. More so, if the only task of the timer is to notify when the time lapses.
The best way is to use the timed wait method.

Pawan: whenever you declare wait method in any method of an object, you have to declare it synchronized, otherwise an unchecked exception will be thrown.

The unchecked exception is IllegalMonitorStateException


apigee, a better way to API!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to set timer for wait() call
 
Similar Threads
Thread.wait() doubts.......
Queing of several request threads
wait(),notify() problem
wait and notify
auto lock application after fixed time