FileDocCategorySizeDatePackage
ChannelClose.javaAPI DocExample4728Mon May 20 00:24:28 BST 2002com.ronsoft.books.nio.channels

ChannelClose

public class ChannelClose extends Object
Test interrupt and async close semantics of InterruptibleChannel objects. This is test code to demonstrate bugs in the 1.4.0 implementation. Use of /dev/tty with FileChannel will probably not work in future releases.
author
Ron Hitchens (ron@ronsoft.com) $Id: ChannelClose.java,v 1.3 2002/05/20 07:24:29 ron Exp $

Fields Summary
Constructors Summary
Methods Summary
public static voidmain(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.ByteChannelnewChannel(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 voidprintln(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 voidtestAsyncClose(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 voidtestInterrupt(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();
		}