package votorola.g.lang; // Copyright 2001-2007, 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.logging.*; import votorola.g.logging.LoggerX; /** An extended Thread, and Thread utilities. */ public final @ThreadSafe class ThreadX extends Thread { public ThreadX() {} public ThreadX( Runnable target ) { super( target ); } public ThreadX( Runnable target, String name ) { super( target, name ); } public ThreadX( ThreadGroup threadGroup, Runnable target, String name ) { super( threadGroup, target, name ); } // ------------------------------------------------------------------------------------ /** Same as {@linkplain Thread#join() join}(), but returns normally if interrupted. * * @return true if the wait ended normally; false if an InterruptedException * occurred. */ public boolean tryJoin() { return tryJoin( ThreadX.this ); } /** Same as {@linkplain Thread#join() join}(), but returns normally if * interrupted. * * @return true if the wait ended normally; false if an InterruptedException * occurred. */ public static boolean tryJoin( Thread thread ) { try { thread.join(); return true; } catch( InterruptedException x ) { return false; } } /** Same as {@linkplain Thread#join(long) join}(), but returns normally if * interrupted. * * @return true if the wait ended normally; false if an InterruptedException * occurred. */ public boolean tryJoin( long milliseconds ) { return tryJoin( ThreadX.this, milliseconds ); } /** Same as {@linkplain Thread#join(long) join}(), but returns normally if * interrupted. * * @return true if the wait ended normally; false if an InterruptedException * occurred. */ public static boolean tryJoin( Thread thread, long milliseconds ) { try { thread.join( milliseconds ); return true; } catch( InterruptedException x ) { return false; } } /** Same as {@linkplain Thread#sleep(long) sleep}(), but an interruption merely cuts * short the sleep. * * @return true if the wait ended normally; false if an InterruptedException * occurred. */ public static boolean trySleep( long milliseconds ) { try { Thread.sleep( milliseconds ); return true; } catch( InterruptedException x ) { return false; } } // ==================================================================================== /** A handler that logs uncaught exceptions and errors using the thread's associated * logger. */ public static @ThreadSafe class UncaughtExceptionLogger implements Thread.UncaughtExceptionHandler { /** Constructs an UncaughtExceptionLogger. * * @see #loggingLevel() */ public UncaughtExceptionLogger( Level _loggingLevel ) { loggingLevel = _loggingLevel; } // FIX by parametizing with a Catcher instead // -------------------------------------------------------------------------------- /** The level at which all uncaught exceptions and errors are logged. */ public final Level loggingLevel() { return loggingLevel; } private final Level loggingLevel; // - T h r e a d . U n c a u g h t - E x c e p t i o n - H a n d l e r ------------ public void uncaughtException( final Thread thread, final Throwable t ) { if( t instanceof ThreadDeath ) throw (ThreadDeath)t; // always let it die, if ThreadDeath ever passed into here LoggerX.i(thread.getClass()).log( loggingLevel, /*message*/"", t ); } } // ==================================================================================== /** A handler that prints stack traces of uncaught exceptions and errors to the * standard error stream. Similar to the default behaviour of * ThreadGroup.{@linkplain java.lang.ThreadGroup#uncaughtException * uncaughtException}. */ public static @ThreadSafe class UncaughtExceptionPrinter implements Thread.UncaughtExceptionHandler { public void uncaughtException( Thread thread, final Throwable t ) { if( t instanceof ThreadDeath ) throw (ThreadDeath)t; // always let it die, if ThreadDeath ever passed into here Catcher.U.printStackTrace( thread, t, System.err ); } } }