KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE.txt file.
7  */

8 package org.apache.avalon.excalibur.thread.impl;
9
10 import org.apache.avalon.excalibur.thread.ThreadControl;
11
12 /**
13  * Default implementation of ThreadControl interface.
14  *
15  * @author <a HREF="mailto:peter@apache.org">Peter Donald</a>
16  */

17 final class DefaultThreadControl
18     implements ThreadControl
19 {
20     ///Thread that this control is associated with
21
private Thread JavaDoc m_thread;
22
23     ///Throwable that caused thread to terminate
24
private Throwable JavaDoc m_throwable;
25
26     /**
27      * Construct thread control for a specific thread.
28      *
29      * @param thread the thread to control
30      */

31     protected DefaultThreadControl( final Thread JavaDoc thread )
32     {
33         m_thread = thread;
34     }
35
36     /**
37      * Wait for specified time for thread to complete it's work.
38      *
39      * @param milliSeconds the duration in milliseconds to wait until the thread has finished work
40      * @exception IllegalStateException if isValid() == false
41      * @exception InterruptedException if another thread has interrupted the current thread.
42      * The interrupted status of the current thread is cleared when this exception
43      * is thrown.
44      */

45     public synchronized void join( final long milliSeconds )
46         throws IllegalStateException JavaDoc, InterruptedException JavaDoc
47     {
48         //final long start = System.currentTimeMillis();
49
wait( milliSeconds );
50         /*
51           if( !isFinished() )
52           {
53           final long now = System.currentTimeMillis();
54           if( start + milliSeconds > now )
55           {
56           final long remaining = milliSeconds - (now - start);
57           join( remaining );
58           }
59           }
60         */

61     }
62
63     /**
64      * Call Thread.interrupt() on thread being controlled.
65      *
66      * @exception IllegalStateException if isValid() == false
67      * @exception SecurityException if caller does not have permission to call interupt()
68      */

69     public synchronized void interupt()
70         throws IllegalStateException JavaDoc, SecurityException JavaDoc
71     {
72         if( !isFinished() )
73         {
74             m_thread.interrupt();
75         }
76     }
77
78     /**
79      * Determine if thread has finished execution
80      *
81      * @return true if thread is finished, false otherwise
82      */

83     public synchronized boolean isFinished()
84     {
85         return ( null == m_thread );
86     }
87
88     /**
89      * Retrieve throwable that caused thread to cease execution.
90      * Only valid when true == isFinished()
91      *
92      * @return the throwable that caused thread to finish execution
93      */

94     public Throwable JavaDoc getThrowable()
95     {
96         return m_throwable;
97     }
98
99     /**
100      * Method called by thread to release control.
101      *
102      * @param throwable Throwable that caused thread to complete (may be null)
103      */

104     protected synchronized void finish( final Throwable JavaDoc throwable )
105     {
106         m_thread = null;
107         m_throwable = throwable;
108         notifyAll();
109     }
110 }
111
Popular Tags