Return-Path: Delivered-To: apmail-jakarta-avalon-cvs-archive@apache.org Received: (qmail 63302 invoked from network); 28 Sep 2002 08:27:11 -0000 Received: from unknown (HELO nagoya.betaversion.org) (192.18.49.131) by daedalus.apache.org with SMTP; 28 Sep 2002 08:27:11 -0000 Received: (qmail 6242 invoked by uid 97); 28 Sep 2002 08:28:05 -0000 Delivered-To: qmlist-jakarta-archive-avalon-cvs@jakarta.apache.org Received: (qmail 6193 invoked by uid 97); 28 Sep 2002 08:28:04 -0000 Mailing-List: contact avalon-cvs-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Avalon CVS List" Reply-To: "Avalon Developers List" Delivered-To: mailing list avalon-cvs@jakarta.apache.org Received: (qmail 6178 invoked by uid 97); 28 Sep 2002 08:28:03 -0000 X-Antivirus: nagoya (v4218 created Aug 14 2002) Date: 28 Sep 2002 08:27:05 -0000 Message-ID: <20020928082705.93359.qmail@icarus.apache.org> From: donaldp@apache.org To: jakarta-avalon-excalibur-cvs@apache.org Subject: cvs commit: jakarta-avalon-excalibur/thread/src/java/org/apache/excalibur/thread/impl WorkerThread.java X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N donaldp 2002/09/28 01:27:05 Added: thread/src/java/org/apache/excalibur/thread/impl WorkerThread.java Log: Add in a WorkerThread that can be used to actually do the work. Derived from original code but decoupled from Avalon, cleaned up extensively and made sub-class friendly. Revision Changes Path 1.1 jakarta-avalon-excalibur/thread/src/java/org/apache/excalibur/thread/impl/WorkerThread.java Index: WorkerThread.java =================================================================== /* * Copyright (C) The Apache Software Foundation. All rights reserved. * * This software is published under the terms of the Apache Software License * version 1.1, a copy of which has been included with this distribution in * the LICENSE.txt file. */ package org.apache.excalibur.thread.impl; import org.apache.excalibur.thread.Executable; import org.apache.excalibur.thread.ThreadControl; /** * This class extends the Thread class to add recyclable functionalities. * * @author Stefano Mazzocchi * @author Peter Donald */ public class WorkerThread extends Thread { /** * The work currentlyy associated with worker (May be null). */ private Executable m_work; /** * The thread control associated with current work. * Should be null if work is null. */ private DefaultThreadControl m_threadControl; /** * True if this thread is alive and not scheduled for shutdown. */ private boolean m_alive; /** * The name of thread. */ private final String m_name; /** * The thread pool this thread is associated with. */ private final AbstractThreadPool m_pool; /** * Allocates a new Worker object. */ protected WorkerThread( final AbstractThreadPool pool, final ThreadGroup group, final String name ) { super( group, "" ); if( null == name ) { throw new NullPointerException( "name" ); } if( null == pool ) { throw new NullPointerException( "pool" ); } m_name = name; m_work = null; m_alive = true; m_pool = pool; setDaemon( false ); } /** * The main execution loop. */ public final synchronized void run() { debug( "starting." ); // Notify the pool this worker started running. //notifyAll(); while( m_alive ) { waitUntilCondition( true ); debug( "running." ); try { preExecute(); m_work.execute(); m_threadControl.finish( null ); } catch( final ThreadDeath threadDeath ) { debug( "thread has died." ); m_threadControl.finish( threadDeath ); // This is to let the thread death propagate to the runtime // enviroment to let it know it must kill this worker throw threadDeath; } catch( final Throwable throwable ) { // Error thrown while working. debug( "error caught: " + throwable ); m_threadControl.finish( throwable ); } finally { debug( "done." ); m_work = null; m_threadControl = null; postExecute(); } //should this be just notify or notifyAll ??? //It seems to resource intensive option to use notify() //notifyAll(); notify(); // recycle ourselves recycleThread(); } } /** * Implement this method to replace thread back into pool. */ protected void recycleThread() { if( m_alive ) { m_pool.releaseWorker( this ); } } /** * Overide this method to execute something after * each bit of "work". */ protected void postExecute() { } /** * Overide this method to execute something before * each bit of "work". */ protected void preExecute() { //TODO: Thread name setting should reuse the //ThreadContext code if ThreadContext used. Thread.currentThread().setName( m_name ); } /** * Set the alive variable to false causing the worker to die. * If the worker is stalled and a timeout generated this call, this method * does not change the state of the worker (that must be destroyed in other * ways). */ public void dispose() { debug( "destroying." ); m_alive = false; waitUntilCondition( false ); } /** * Set the Work code this Worker must * execute and notifies its thread to do it. */ protected synchronized ThreadControl execute( final Executable work ) { m_work = work; m_threadControl = new DefaultThreadControl( this ); debug( "notifying this worker." ); notify(); return m_threadControl; } /** * Set the Work code this Worker must * execute and notifies its thread to do it. Wait * until the executable has finished before returning. */ protected synchronized void executeAndWait( final Executable work ) { execute( work ); waitUntilCondition( false ); } /** * Wait until the worker either has work or doesn't have work. * * @param hasWork true if waiting till work is present, false otherwise */ private synchronized void waitUntilCondition( final boolean hasWork ) { while( hasWork == ( null == m_work ) ) { try { debug( "waiting." ); wait(); debug( "notified." ); } catch( final InterruptedException ie ) { } } } /** * Write a debug message. * A Noop oin this implementation. Subclasses can overide * to actually do some logging. * * @param message the message to write out */ protected void debug( final String message ) { if( false ) { final String output = getName() + ": " + message; System.out.println( output ); } } } -- To unsubscribe, e-mail: For additional commands, e-mail: