package votorola.g.util.concurrent; // Copyright 2006, 2009, Michael Allan. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Votorola Software"), to deal in the Votorola Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Votorola Software, and to permit persons to whom the Votorola Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Votorola Software. THE VOTOROLA SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE VOTOROLA SOFTWARE OR THE USE OR OTHER DEALINGS IN THE VOTOROLA SOFTWARE. import java.util.concurrent.*; import votorola.g.lang.*; import votorola.g.logging.*; /** An extended ScheduledThreadPoolExecutor. */ public @ThreadSafe final class ScheduledThreadPoolExecutorX extends ScheduledThreadPoolExecutor { /** Constructs a ScheduledThreadPoolExecutorX with a no-op catcher. * * @see #getCorePoolSize() * @see #getThreadFactory() * @see #catcher() */ public ScheduledThreadPoolExecutorX( int _corePoolSize, ThreadFactory _threadFactory ) { super( _corePoolSize, _threadFactory ); catcher = Catcher00.i(); } /** Constructs a ScheduledThreadPoolExecutorX. * * @see #getCorePoolSize() * @see #getThreadFactory() * @see #catcher() */ public ScheduledThreadPoolExecutorX( int _corePoolSize, ThreadFactory _threadFactory, Catcher _catcher ) { super( _corePoolSize, _threadFactory ); catcher = _catcher; if( !ThreadSafe.U.isThreadSafe( catcher, "catchException", Object.class, Exception.class )) throw new IllegalArgumentException( "catcher.catchException(-) is not thread-safe" ); } // ------------------------------------------------------------------------------------ /** The catcher for all execution exceptions. Normally, it ought not to rethrow * whatever is caught, because the executor will already be aware of it. */ public Catcher catcher() { return catcher; } private final Catcher catcher; // - T h r e a d - P o o l - E x e c u t o r ------------------------------------------ protected @Override void afterExecute( final Runnable r, Throwable t ) { super.afterExecute( r, t ); if( r instanceof FutureTask ) { if( t != null ) LoggerX.i(getClass()).config( "FutureTask is no longer hiding exceptions from afterExecute()" ); // code needs updating else { final FutureTask futureTask = (FutureTask)r; if( futureTask.isDone() ) // returns false when completed normally (API docs are lying), in which case futureTask.get() hangs, even with a timeout { try { futureTask.get(); // expose the hidden exception, if any } catch( ExecutionException x ) { t = x.getCause(); } // cannot expose the wrapped runnable though, no such method catch( Throwable x ) { t = new Exception( "unexpected exception from executor", x ); } } } } if( t != null ) { if( t instanceof Error ) catcher.catchError( r, (Error)t ); else if( t instanceof Exception ) catcher.catchException( r, (Exception)t ); else assert false; } } }