Results 1 to 2 of 2
  1. #1
    alexalex1234 is offline Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Default WAIT method question

    I'm using 'public final void wait(long timeout, int nanos)' on a monitor.

    Reading the javadoc I've found the following sentences:
    * Another thread notifies threads waiting on this object's monitor to wake up either through a call to the notify method or the notifyAll method.
    * The timeout period, specified by timeout milliseconds plus nanos nanoseconds arguments, has elapsed.

    How can I differentiate between the two cases?
    If the timeout period has elapsed I want to throw an exception and in the other case I want to continue to the following steps.

    Thank you.
    Alex

  2. #2
    honnza is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default

    You may either:
    -measure the time spent waiting, and if it's big enough, throw an exception. This is simple to implement but you could get some false positives. If your request is meant to be a debugging facility, it is likely just fine, if you keep in mind the thread might in fact have been notified.
    -instead of timed wait, use an infinite wait + a separate watchdog thread. If the watchdog interrupts your thread, you get an interruptedException. If the watchdog interrups the other thread after returning from wait, the thread gets its interrupt flag set - you can check the flag after resetting the watchdog.
    -create a new watchdog thread every time you wait. This causes considerable performance penalty but it simplifies the implementation a bit.

    There might other option I haven't thought of, please share your ideas.

Similar Threads

  1. Wait method and sleep method
    By chathura87 in forum New To Java
    Replies: 3
    Last Post: 03-01-2011, 06:04 PM
  2. Why would a String class need a wait() method?
    By javanb in forum New To Java
    Replies: 8
    Last Post: 12-06-2010, 02:36 PM
  3. Wait() Question
    By rsvr in forum Threads and Synchronization
    Replies: 3
    Last Post: 04-27-2010, 04:39 PM
  4. need help with question(method & array)
    By highschool in forum New To Java
    Replies: 5
    Last Post: 02-10-2010, 06:06 PM
  5. method question
    By xplayerr in forum New To Java
    Replies: 7
    Last Post: 11-11-2009, 05:13 PM

Tags for this Thread

Posting Permissions

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