Forward Computing and Control Pty. Ltd.
Threads Package V1.0.3

2005/04/11

au.com.forward.threads
Class TimeOut

java.lang.Object
  extended by java.util.Date
      extended by au.com.forward.threads.TimeOut
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.lang.Comparable<java.util.Date>

public class TimeOut
extends java.util.Date

This class sets a timeout time which can by used by mulitple join() statements to specify the total time required for a number of threads to complete. Typical use is:-

thread_1.start();  // start all the threads
thread_2.start();
thread_3.start();

TimeOut timeOut = new TimeOut(5000); // time out expires in 5 seconds from now

ThreadReturn.join(thread_1,timeOut.timeRemaining()); 
ThreadReturn.join(thread_2,timeOut.timeRemaining()); 
ThreadReturn.join(thread_3,timeOut.timeRemaining()); 
This sets the timeout at 5 seconds from the time the TimeOut constructor is executed. This sets an absolute end time of now + 5 seconds.

The first join() statement receives timeout arguement of the absolute end time - now in milliseconds. When this join() returns the following join() receives a timeout arguement of only the remaining time (in milliseconds) and so on. This ensures that each of the three threads have at most 5 seconds to complete their tasks.

If the absolute end time has been passed then timeRemaining() returns 1 millisecond as a minimum value because 0 causes join() to wait forever and negative numbers are not valid arguements for join(). This means that maximum timeout is actually 5 sec + 2 milliseconds for this example or in general
maximum timeout = constructor timeout + (no. of thread joins - 1) * 1mS

Author:
matthew ford
See Also:
Serialized Form

Constructor Summary
TimeOut(long milliseconds)
          Creates a new timeout with an absolute time of now + milliseconds
 
Method Summary
 long timeRemaining()
          Returns the number of milliseconds remaining from now.
 
Methods inherited from class java.util.Date
after, before, clone, compareTo, equals, getDate, getDay, getHours, getMinutes, getMonth, getSeconds, getTime, getTimezoneOffset, getYear, hashCode, parse, setDate, setHours, setMinutes, setMonth, setSeconds, setTime, setYear, toGMTString, toLocaleString, toString, UTC
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

TimeOut

public TimeOut(long milliseconds)
Creates a new timeout with an absolute time of now + milliseconds

Parameters:
milliseconds - the timeout.
Method Detail

timeRemaining

public long timeRemaining()
Returns the number of milliseconds remaining from now.

Returns:
the number of milliseconds remaining from now or 1 if none left

Forward Computing and Control Pty. Ltd.
Threads Package V1.0.3

2005/04/11

Copyright ©2002-5, Forward Computing and Control Pty. Ltd
ACN 003 669 994   NSW Australia,   All Rights Reserved.