001package 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.
002
003import java.util.logging.*;
004import votorola.g.logging.LoggerX;
005
006
007/** An extended Thread, and Thread utilities.
008  */
009public final @ThreadSafe class ThreadX extends Thread
010{
011
012
013    public ThreadX() {}
014
015
016
017    public ThreadX( Runnable target ) { super( target ); }
018
019
020
021    public ThreadX( Runnable target, String name ) { super( target, name ); }
022
023
024
025    public ThreadX( ThreadGroup threadGroup, Runnable target, String name )
026    {
027        super( threadGroup, target, name );
028    }
029
030
031
032   // ------------------------------------------------------------------------------------
033
034
035    /** Same as {@linkplain Thread#join() join}(), but returns normally if interrupted.
036      *
037      *     @return true if the wait ended normally; false if an InterruptedException
038      *       occurred.
039      */
040    public boolean tryJoin() { return tryJoin( ThreadX.this ); }
041
042
043
044        /** Same as {@linkplain Thread#join() join}(), but returns normally if
045          * interrupted.
046          *
047          *     @return true if the wait ended normally; false if an InterruptedException
048          *       occurred.
049          */
050        public static boolean tryJoin( Thread thread )
051        {
052            try
053            {
054                thread.join();
055                return true;
056            }
057            catch( InterruptedException x ) { return false; }
058        }
059
060
061
062    /** Same as {@linkplain Thread#join(long) join}(), but returns normally if
063      * interrupted.
064      *
065      *     @return true if the wait ended normally; false if an InterruptedException
066      *       occurred.
067      */
068    public boolean tryJoin( long milliseconds ) { return tryJoin( ThreadX.this, milliseconds ); }
069
070
071
072        /** Same as {@linkplain Thread#join(long) join}(), but returns normally if
073          * interrupted.
074          *
075          *     @return true if the wait ended normally; false if an InterruptedException
076          *       occurred.
077          */
078        public static boolean tryJoin( Thread thread, long milliseconds )
079        {
080            try
081            {
082                thread.join( milliseconds );
083                return true;
084            }
085            catch( InterruptedException x ) { return false; }
086        }
087
088
089
090    /** Same as {@linkplain Thread#sleep(long) sleep}(), but an interruption merely cuts
091      * short the sleep.
092      *
093      *     @return true if the wait ended normally; false if an InterruptedException
094      *       occurred.
095      */
096    public static boolean trySleep( long milliseconds )
097    {
098        try
099        {
100            Thread.sleep( milliseconds );
101            return true;
102        }
103        catch( InterruptedException x ) { return false; }
104    }
105
106
107
108   // ====================================================================================
109
110
111    /** A handler that logs uncaught exceptions and errors using the thread's associated
112      * logger.
113      */
114    public static @ThreadSafe class UncaughtExceptionLogger
115      implements Thread.UncaughtExceptionHandler
116    {
117
118
119        /** Constructs an UncaughtExceptionLogger.
120          *
121          *     @see #loggingLevel()
122          */
123        public UncaughtExceptionLogger( Level _loggingLevel ) { loggingLevel = _loggingLevel; }
124          // FIX by parametizing with a Catcher instead
125
126
127
128       // --------------------------------------------------------------------------------
129
130
131        /** The level at which all uncaught exceptions and errors are logged.
132          */
133        public final Level loggingLevel() { return loggingLevel; }
134
135
136            private final Level loggingLevel;
137
138
139
140       // - 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 ------------
141
142
143        public void uncaughtException( final Thread thread, final Throwable t )
144        {
145            if( t instanceof ThreadDeath ) throw (ThreadDeath)t; // always let it die, if ThreadDeath ever passed into here
146
147            LoggerX.i(thread.getClass()).log( loggingLevel, /*message*/"", t );
148        }
149
150
151
152    }
153
154
155
156   // ====================================================================================
157
158
159    /** A handler that prints stack traces of uncaught exceptions and errors to the
160      * standard error stream.  Similar to the default behaviour of
161      * ThreadGroup.{@linkplain java.lang.ThreadGroup#uncaughtException
162      * uncaughtException}.
163      */
164    public static @ThreadSafe class UncaughtExceptionPrinter
165      implements Thread.UncaughtExceptionHandler
166    {
167        public void uncaughtException( Thread thread, final Throwable t )
168        {
169            if( t instanceof ThreadDeath ) throw (ThreadDeath)t; // always let it die, if ThreadDeath ever passed into here
170
171            Catcher.U.printStackTrace( thread, t, System.err );
172        }
173    }
174
175
176}