001package 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.
002
003import java.util.concurrent.*;
004import votorola.g.lang.*;
005import votorola.g.logging.*;
006
007
008/** An extended ScheduledThreadPoolExecutor.
009  */
010public @ThreadSafe final class ScheduledThreadPoolExecutorX extends ScheduledThreadPoolExecutor
011{
012
013
014    /** Constructs a ScheduledThreadPoolExecutorX with a no-op catcher.
015      *
016      *     @see #getCorePoolSize()
017      *     @see #getThreadFactory()
018      *     @see #catcher()
019      */
020    public ScheduledThreadPoolExecutorX( int _corePoolSize, ThreadFactory _threadFactory )
021    {
022        super( _corePoolSize, _threadFactory );
023        catcher = Catcher00.i();
024    }
025
026
027
028    /** Constructs a ScheduledThreadPoolExecutorX.
029      *
030      *     @see #getCorePoolSize()
031      *     @see #getThreadFactory()
032      *     @see #catcher()
033      */
034    public ScheduledThreadPoolExecutorX( int _corePoolSize, ThreadFactory _threadFactory,
035      Catcher<Runnable> _catcher )
036    {
037        super( _corePoolSize, _threadFactory );
038        catcher = _catcher;
039        if( !ThreadSafe.U.isThreadSafe( catcher, "catchException", Object.class, Exception.class )) throw new IllegalArgumentException( "catcher.catchException(-) is not thread-safe" );
040    }
041
042
043
044   // ------------------------------------------------------------------------------------
045
046
047    /** The catcher for all execution exceptions.  Normally, it ought not to rethrow
048      * whatever is caught, because the executor will already be aware of it.
049      */
050    public Catcher<Runnable> catcher() { return catcher; }
051
052
053        private final Catcher<Runnable> catcher;
054
055
056
057   // - T h r e a d - P o o l - E x e c u t o r ------------------------------------------
058
059
060    protected @Override void afterExecute( final Runnable r, Throwable t )
061    {
062        super.afterExecute( r, t );
063        if( r instanceof FutureTask<?> )
064        {
065            if( t != null ) LoggerX.i(getClass()).config( "FutureTask is no longer hiding exceptions from afterExecute()" ); // code needs updating
066            else
067            {
068                final FutureTask<?> futureTask = (FutureTask<?>)r;
069                if( futureTask.isDone() ) // returns false when completed normally (API docs are lying), in which case futureTask.get() hangs, even with a timeout
070                {
071                    try
072                    {
073                        futureTask.get(); // expose the hidden exception, if any
074                    }
075                    catch( ExecutionException x ) { t = x.getCause(); } // cannot expose the wrapped runnable though, no such method
076                    catch( Throwable x )
077                    {
078                        t = new Exception( "unexpected exception from executor", x );
079                    }
080                }
081            }
082        }
083        if( t != null )
084        {
085            if( t instanceof Error ) catcher.catchError( r, (Error)t );
086            else if( t instanceof Exception ) catcher.catchException( r, (Exception)t );
087            else assert false;
088        }
089    }
090
091
092
093}
094