package textbender.g.lang; // Copyright 2001-2006, Michael Allan. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Textbender Software"), to deal in the Textbender Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Textbender Software, and to permit persons to whom the Textbender 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 Textbender Software. THE TEXTBENDER 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 TEXTBENDER SOFTWARE OR THE USE OR OTHER DEALINGS IN THE TEXTBENDER SOFTWARE. import textbender.g.util.logging.LoggerX; /** An extended Thread, and Thread utilities. */ public final 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 occured */ 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 occured */ 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 occured */ 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 occured */ 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 occured */ public static boolean trySleep( long milliseconds ) { try { sleep( milliseconds ); return true; } catch( InterruptedException x ) { return false; } } /** A handler that logs uncaught errors and exceptions * using the thread's associated logger. */ public static final Thread.UncaughtExceptionHandler UNCAUGHT_EXCEPTION_LOGGER = new 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 LoggerX.i(thread.getClass()).log( LoggerX.WARNING, /*message*/"", t ); } }; /** A handler that prints stack traces of uncaught errors and exceptions * to the standard error stream. Similar to the default behaviour * of ThreadGroup.{@linkplain java.lang.ThreadGroup#uncaughtException uncaughtException}. */ public static final Thread.UncaughtExceptionHandler UNCAUGHT_EXCEPTION_PRINTER = new 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.X.printStackTrace( thread, t, System.err ); } }; }