IOTimerpublic class IOTimer extends Thread Timer to interrupt a long-running call (like read, write, hence the name). |
Fields Summary |
---|
Thread | timedThreadThe Thread we are timing. | int | timeHow 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 void | main(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 void | run()
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();
|
|