KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > util > thread > DefaultThreadControl


1 /***********************************************************************
2  * Copyright (c) 1999-2004 The Apache Software Foundation. *
3  * All rights reserved. *
4  * ------------------------------------------------------------------- *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you *
6  * may not use this file except in compliance with the License. You *
7  * may obtain a copy of the License at: *
8  * *
9  * http://www.apache.org/licenses/LICENSE-2.0 *
10  * *
11  * Unless required by applicable law or agreed to in writing, software *
12  * distributed under the License is distributed on an "AS IS" BASIS, *
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or *
14  * implied. See the License for the specific language governing *
15  * permissions and limitations under the License. *
16  ***********************************************************************/

17
18 package org.apache.james.util.thread;
19
20 import org.apache.avalon.excalibur.thread.ThreadControl;
21
22 /**
23  * Default implementation of ThreadControl interface.
24  *
25  */

26 final class DefaultThreadControl
27    implements ThreadControl
28 {
29     ///Thread that this control is associated with
30
private Thread JavaDoc m_thread;
31
32     ///Throwable that caused thread to terminate
33
private Throwable JavaDoc m_throwable;
34
35     /**
36      * Construct thread control for a specific thread.
37      *
38      * @param thread the thread to control
39      */

40        protected DefaultThreadControl( final Thread JavaDoc thread )
41        {
42            m_thread = thread;
43        }
44
45     /**
46      * Wait for specified time for thread to complete it's work.
47      *
48      * @param milliSeconds the duration in milliseconds to wait until the thread has finished work
49      * @throws IllegalStateException if isValid() == false
50      * @throws InterruptedException if another thread has interrupted the current thread.
51      * The interrupted status of the current thread is cleared when this exception
52      * is thrown.
53      */

54        public synchronized void join( final long milliSeconds )
55                throws IllegalStateException JavaDoc, InterruptedException JavaDoc
56        {
57         //final long start = System.currentTimeMillis();
58
wait( milliSeconds );
59         /*
60           if( !isFinished() )
61           {
62           final long now = System.currentTimeMillis();
63           if( start + milliSeconds > now )
64           {
65           final long remaining = milliSeconds - (now - start);
66           join( remaining );
67           }
68           }
69         */

70        }
71
72     /**
73      * Call Thread.interrupt() on thread being controlled.
74      *
75      * @throws IllegalStateException if isValid() == false
76      * @throws SecurityException if caller does not have permission to call interupt()
77      */

78        public synchronized void interupt()
79                throws IllegalStateException JavaDoc, SecurityException JavaDoc
80        {
81            if( !isFinished() )
82            {
83                m_thread.interrupt();
84            }
85        }
86
87     /**
88      * Determine if thread has finished execution
89      *
90      * @return true if thread is finished, false otherwise
91      */

92        public synchronized boolean isFinished()
93        {
94            return ( null == m_thread );
95        }
96
97     /**
98      * Retrieve throwable that caused thread to cease execution.
99      * Only valid when true == isFinished()
100      *
101      * @return the throwable that caused thread to finish execution
102      */

103        public Throwable JavaDoc getThrowable()
104        {
105            return m_throwable;
106        }
107
108     /**
109      * Method called by thread to release control.
110      *
111      * @param throwable Throwable that caused thread to complete (may be null)
112      */

113        protected synchronized void finish( final Throwable JavaDoc throwable )
114        {
115            m_thread = null;
116            m_throwable = throwable;
117            notifyAll();
118        }
119 }
120
Popular Tags