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

SelectSocketsThreadPool

public class SelectSocketsThreadPool extends SelectSockets
Specialization of the SelectSockets class which uses a thread pool to service channels. The thread pool is an ad-hoc implementation quicky lashed togther in a few hours for demonstration purposes. It's definitely not production quality. Created May 2002
author
Ron Hitchens (ron@ronsoft.com)
version
$Id: SelectSocketsThreadPool.java,v 1.5 2002/05/20 07:24:29 ron Exp $

Fields Summary
private static final int
MAX_THREADS
private ThreadPool
pool
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] argv)


	// -------------------------------------------------------------

	      
		 
	
		new SelectSocketsThreadPool().go (argv);
	
protected voidreadDataFromSocket(java.nio.channels.SelectionKey key)
Sample data handler method for a channel with data ready to read. This method is invoked from the go() method in the parent class. This handler delegates to a worker thread in a thread pool to service the channel, then returns immediately.

param
key A SelectionKey object representing a channel determined by the selector to be ready for reading. If the channel returns an EOF condition, it is closed here, which automatically invalidates the associated key. The selector will then de-register the channel on the next select call.

		WorkerThread worker = pool.getWorker();

		if (worker == null) {
			// No threads available, do nothing, the selection
			// loop will keep calling this method until a
			// thread becomes available.  This design could
			// be improved.
			return;	
		}

		// invoking this wakes up the worker thread then returns
		worker.serviceChannel (key);