KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > excalibur > thread > impl > DefaultThreadControl


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

17 package org.apache.excalibur.thread.impl;
18
19 import org.apache.excalibur.thread.ThreadControl;
20
21 /**
22  * Default implementation of ThreadControl interface.
23  * Is used by worker thread to supply control information to the
24  * clients of thread pool.
25  *
26  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
27  */

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

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

56     public synchronized void join( final long milliSeconds )
57         throws IllegalStateException JavaDoc, InterruptedException JavaDoc
58     {
59         if (! isFinished() )
60         {
61             m_thread.join(milliSeconds);
62         }
63     }
64
65     public void interupt()
66         throws IllegalStateException JavaDoc, SecurityException JavaDoc
67     {
68         interrupt();
69     }
70
71     /**
72      * Call Thread.interrupt() on thread being controlled.
73      *
74      * @throws IllegalStateException if isValid() == false
75      * @throws SecurityException if caller does not have permission to call interupt()
76      */

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

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

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

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