Methods Summary |
---|
public static void | main(java.lang.String[] argv)
int port = 0;
if (argv.length > 0) {
port = Integer.parseInt (argv [0]);
}
Thread.currentThread().setName ("Main");
System.out.println ("--- Testing Interrupt Thread ---");
testInterrupt (newChannel (port));
System.out.println ("");
System.out.println ("--- Testing Async Channel Close ---");
testAsyncClose (newChannel (port));
|
private static java.nio.channels.ByteChannel | newChannel(int netPort)
if (netPort == 0) {
FileInputStream fis = new FileInputStream ("/dev/tty");
return (fis.getChannel());
} else {
ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.socket().bind (new InetSocketAddress (netPort));
System.out.print ("Waiting for connection on port "
+ netPort + "...");
System.out.flush();
ByteChannel channel = ssc.accept();
ssc.close();
System.out.println ("Got it");
return (channel);
}
|
private static void | println(java.lang.String s)
SimpleDateFormat df = new SimpleDateFormat (
"HH:mm:ss.SSS");
String ds = df.format (new Date());
System.out.println (ds + " ("
+ Thread.currentThread().getName()
+ "): " + s);
|
private static void | testAsyncClose(java.nio.channels.ByteChannel channel)
Sleeper sleeper = new Sleeper (channel);
sleeper.setName ("Sleeper");
try {
ByteBuffer buffer = ByteBuffer.allocate (100);
println ("starting sleeper thread");
sleeper.start();
println ("waiting for sleeper thread to issue read...");
Thread.sleep (1000);
println ("closing channel");
channel.close();
println ("joining sleeper "
+ "(you may need to type something)...");
sleeper.join();
} catch (Throwable e) {
println ("Caught " + e);
}
|
private static void | testInterrupt(java.nio.channels.ByteChannel channel)
Sleeper sleeper = new Sleeper (channel);
sleeper.setName ("Sleeper");
try {
ByteBuffer buffer = ByteBuffer.allocate (100);
println ("starting sleeper thread");
sleeper.start();
println ("waiting for sleeper thread to issue read...");
Thread.sleep (1000);
println ("interrupting sleeper");
sleeper.interrupt();
Thread.sleep (100); // avoid race with sleeper
println ("issuing read on interrupted channel...");
channel.read (buffer);
println ("read completed OK");
} catch (Throwable e) {
println ("Caught " + e);
} finally {
println ("closing channel");
channel.close();
println ("joining sleeper "
+ "(you may need to type something)...");
sleeper.join();
}
|