Summary

Chapter 14 - Waiting for the Full Timeout

Java Thread Programming
Paul Hyde
  Copyright 1999 Sams Publishing

Chapter 14: Waiting for the Full Timeout
Overview
In Java, two threads can communicate with each other through the wait-notify mechanism (see Chapter 8, Inter-thread Communication ). When a thread invokes one of the wait() methods of Object , an optional timeout value can be specified. The thread does not return from the wait(long msTimeout) call until it is notified by another thread, the specified millisecond timeout has elapsed, or it is interrupted. It is obvious when it has been interrupted as an InterruptedException is thrown. But other than that, wait() does not return any information as to whether it was notified or just timed out.
There will be times when youll want to write code that waits up to a specified amount of time for a condition to be met. The waiting thread might be notified many times before the condition is met. Imagine that you want a thread to wait for an integer value to increase from 5 to a minimum of 20 . Every time the value is changed by another thread, the waiting thread is notified. The notification doesnt necessarily mean that value being waited for has been met, but simply that the value has been changed. The thread waiting for the condition to be met will return from wait(long msTimeout) either because the timeout has expired , or because it has been notified. In this chapter, Ill show you a technique for differentiating between these two possibilities to ensure that the full timeout elapses when required.

Toc


Java Thread Programming
Java Thread Programming
ISBN: 0672315858
EAN: 2147483647
Year: 2005
Pages: 149
Authors: Paul Hyde

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net