/** * Returns an NGSession from the pool, or creates one if necessary * @return an NGSession ready to work */ NGSession take() { NGSession result; synchronized(lock) { if (poolEntries == 0) { result = new NGSession(this, server); result.start(); } else { --poolEntries; result = pool[poolEntries]; } } return (result); }
/** * Updates the current thread name (useful for debugging). */ private void updateThreadName(String detail) { setName("NGSession " + instanceNumber + ": " + ((detail == null) ? "(idle)" : detail)); } }
/** * Returns an NGSession to the pool. The pool may choose to shutdown * the thread if the pool is full * @param session the NGSession to return to the pool */ void give(NGSession session) { boolean shutdown = false; synchronized(lock) { if (done || poolEntries == poolSize) { shutdown = true; } else { pool[poolEntries] = session; ++poolEntries; } } if (shutdown) session.shutdown(); }
updateThreadName(null); Socket socket = nextSocket(); while (socket != null) { try { updateThreadName(socket.getInetAddress().getHostAddress() + ": " + command); ((ThreadLocalPrintStream) System.err).init(null); updateThreadName(null); sessionPool.give(this); socket = nextSocket();
/** * Shuts down the pool. Running nails are allowed to finish. */ void shutdown() { done = true; synchronized(lock) { while (poolEntries > 0) { take().shutdown(); } } }