KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > thread > DedicatedTaskRunner


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

18 package org.apache.activemq.thread;
19
20
21 /**
22  *
23  * @version $Revision: 1.1 $
24  */

25 class DedicatedTaskRunner implements TaskRunner {
26
27     private final Task task;
28     private final Thread JavaDoc thread;
29     
30     private final Object JavaDoc mutex = new Object JavaDoc();
31     private boolean threadTerminated;
32     private boolean pending;
33     private boolean shutdown;
34     
35     public DedicatedTaskRunner(Task task, String JavaDoc name, int priority, boolean daemon) {
36         this.task = task;
37         thread = new Thread JavaDoc(name) {
38             public void run() {
39                 runTask();
40             }
41         };
42         thread.setDaemon(daemon);
43         thread.setName(name);
44         thread.setPriority(priority);
45         thread.start();
46     }
47     
48     /**
49      */

50     public void wakeup() throws InterruptedException JavaDoc {
51         synchronized( mutex ) {
52             if( shutdown )
53                 return;
54             pending=true;
55             mutex.notifyAll();
56         }
57     }
58
59     /**
60      * shut down the task
61      * @param timeout
62      * @throws InterruptedException
63      */

64     public void shutdown(long timeout) throws InterruptedException JavaDoc{
65         synchronized(mutex){
66             shutdown=true;
67             pending=true;
68             mutex.notifyAll();
69
70             // Wait till the thread stops.
71
if(!threadTerminated){
72                 mutex.wait(timeout);
73             }
74         }
75     }
76     
77     /**
78      * shut down the task
79      * @throws InterruptedException
80      */

81     public void shutdown() throws InterruptedException JavaDoc{
82         shutdown(0);
83     }
84     
85     private void runTask() {
86         
87         try {
88             while( true ) {
89              
90                 synchronized (mutex) {
91                     pending=false;
92                     if( shutdown ) {
93                         return;
94                     }
95                 }
96                 
97                 if( !task.iterate() ) {
98                     // wait to be notified.
99
synchronized (mutex) {
100                         while( !pending ) {
101                             mutex.wait();
102                         }
103                     }
104                 }
105                 
106             }
107             
108         } catch (InterruptedException JavaDoc e) {
109             // Someone really wants this thread to die off.
110
Thread.currentThread().interrupt();
111         } finally {
112             // Make sure we notify any waiting threads that thread
113
// has terminated.
114
synchronized (mutex) {
115                 threadTerminated=true;
116                 mutex.notifyAll();
117             }
118         }
119     }
120 }
121
Popular Tags