FileDocCategorySizeDatePackage
IOTimer.javaAPI DocExample3694Sun Feb 08 21:34:06 GMT 2004None

IOTimer

public class IOTimer extends Thread
Timer to interrupt a long-running call (like read, write, hence the name).
author
Ian F. Darwin, http://www.darwinsys.com/

Fields Summary
Thread
timedThread
The Thread we are timing.
int
time
How long to give the Thread
Constructors Summary
public IOTimer(Thread t, int mSec)

		System.out.println("IOTimer.<init>: Thread " + t);
		setPriority(MAX_PRIORITY);
		timedThread = t;
		time = mSec;
	
Methods Summary
public static voidmain(java.lang.String[] ap)
Simple test case

		byte b[] = new byte[10];
		System.out.println("Creating IOTimer");
		new IOTimer(Thread.currentThread(), 1000).start();
		System.out.println("Starting read");
		try {
			// This read will block, unless you type something in
			// the console window (and you have to be pretty quick!).
			System.in.read(b);

		// Cannot catch InterruptedException, as read() doesn't declare it.
		} catch (Exception e) {
			System.out.println("Caught " + e);
		}
		System.out.println("All done");
	
public voidrun()

		System.out.println("About to sleep for " + time + "mSec");
		try {
			sleep(time);
		} catch (InterruptedException ie) {
			System.out.println("You interrupted my sleep!");
			return;
		}
		// if we are still here, the timer went off
		System.out.println("Will now interrupt sleep of " + timedThread);
		timedThread.interrupt();