KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > dream > control > activity > task > thread > ThreadTask


1 /**
2  * Dream
3  * Copyright (C) 2003-2004 INRIA Rhone-Alpes
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact : dream@objectweb.org
20  *
21  * Initial developer(s): Vivien Quema
22  * Contributor(s):
23  */

24
25 package org.objectweb.dream.control.activity.task.thread;
26
27 import java.util.ArrayList JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30
31 import org.objectweb.dream.control.activity.task.Task;
32 import org.objectweb.dream.control.activity.task.TaskLifeCycleController;
33 import org.objectweb.dream.control.activity.task.TaskStoppedListener;
34 import org.objectweb.dream.util.Error;
35 import org.objectweb.fractal.api.NoSuchInterfaceException;
36 import org.objectweb.fractal.api.control.IllegalLifeCycleException;
37 import org.objectweb.fractal.julia.control.lifecycle.ChainedIllegalLifeCycleException;
38 import org.objectweb.util.monolog.api.BasicLevel;
39
40 /**
41  * Basic implementation of a Dream thread.
42  *
43  * @see org.objectweb.dream.control.activity.task.thread.AbstractThreadTask
44  */

45 public class ThreadTask extends AbstractThreadTask
46     implements
47       Runnable JavaDoc,
48       TaskLifeCycleController
49 {
50   protected Object JavaDoc threadTaskLock = new Object JavaDoc();
51   protected Thread JavaDoc thread = null;
52   protected List JavaDoc taskStoppedListeners = new ArrayList JavaDoc();
53
54   protected boolean executing;
55
56   // ---------------------------------------------------------------------------
57
// Implementation of the AbstractThreadTask abstract methods
58
// ---------------------------------------------------------------------------
59

60   /**
61    * @see AbstractThreadTask#isExecuting()
62    */

63   protected boolean isExecuting()
64   {
65     return executing;
66   }
67
68   /**
69    * @see AbstractThreadTask#setExecuting(boolean)
70    */

71   protected void setExecuting(boolean b)
72   {
73     executing = b;
74   }
75
76   // ---------------------------------------------------------------------------
77
// Implementation of the Runnable interface
78
// ---------------------------------------------------------------------------
79

80   /**
81    * @see java.lang.Runnable#run()
82    */

83   public void run()
84   {
85     logger.log(BasicLevel.DEBUG, "Begin of the run method");
86     try
87     {
88       execute(null);
89     }
90     catch (InterruptedException JavaDoc e)
91     {
92       logger.log(BasicLevel.DEBUG, "Interrupted");
93     }
94     finally
95     {
96       synchronized (threadTaskLock)
97       {
98         try
99         {
100           super.stopFc();
101         }
102         catch (IllegalLifeCycleException ignored)
103         {
104           // throws nothing
105
}
106         Task taskItf = null;
107         try
108         {
109           taskItf = (Task) weaveableC.getFcInterface("task");
110         }
111         catch (NoSuchInterfaceException ignored)
112         {
113           // can't happend
114
}
115         Iterator JavaDoc iter = taskStoppedListeners.iterator();
116         while (iter.hasNext())
117         {
118           ((TaskStoppedListener) iter.next()).taskStopped(taskItf);
119           iter.remove();
120         }
121         thread = null;
122         try
123         {
124           // set component in stopped state.
125
super.stopFc();
126         }
127         catch (IllegalLifeCycleException e1)
128         {
129           // can't happend.
130
}
131         logger.log(BasicLevel.DEBUG, "End of the run method");
132       }
133     }
134   }
135
136   // ---------------------------------------------------------------------------
137
// Implementation of the LifeCycleController interface
138
// ---------------------------------------------------------------------------
139

140   /**
141    * @see org.objectweb.fractal.api.control.LifeCycleController#startFc()
142    */

143   public void startFc() throws IllegalLifeCycleException
144   {
145     synchronized (threadTaskLock)
146     {
147       if (getFcState() == STARTED)
148       {
149         return;
150       }
151
152       // sanity check.
153
if (thread != null)
154       {
155         Error.bug(logger, new IllegalStateException JavaDoc(
156             "Thread component is not in start state, but has a thread"));
157       }
158
159       super.startFc();
160       executing = true;
161       thread = new Thread JavaDoc(this);
162       logger.log(BasicLevel.DEBUG, "Starting the thread task");
163       thread.start();
164     }
165   }
166
167   /**
168    * @see org.objectweb.fractal.api.control.LifeCycleController#stopFc()
169    */

170   public void stopFc() throws IllegalLifeCycleException
171   {
172     Thread JavaDoc t;
173     synchronized (threadTaskLock)
174     {
175       if (getFcState() == STOPPED)
176       {
177         return;
178       }
179
180       // sanity check.
181
if (thread == null)
182       {
183         Error.bug(logger, new IllegalStateException JavaDoc(
184             "Thread component is in start state, but has no thread"));
185       }
186
187       executing = false;
188       logger.log(BasicLevel.DEBUG, "Interrupting the thread task");
189       thread.interrupt();
190       t = thread;
191     }
192     try
193     {
194       logger.log(BasicLevel.DEBUG, "Join thread task");
195       t.join();
196     }
197     catch (InterruptedException JavaDoc e)
198     {
199       throw new ChainedIllegalLifeCycleException(e, weaveableC,
200           "Interrupted while waiting for the end of the thread.");
201     }
202     logger.log(BasicLevel.DEBUG, "Thread task stop");
203   }
204
205   /**
206    * @see TaskLifeCycleController#asyncStop(TaskStoppedListener)
207    */

208   public void asyncStop(TaskStoppedListener listener)
209   {
210     synchronized (threadTaskLock)
211     {
212       if (getFcState() == STOPPED)
213       {
214         // thread is already stopped
215
Task taskItf = null;
216         try
217         {
218           taskItf = (Task) weaveableC.getFcInterface("task");
219         }
220         catch (NoSuchInterfaceException ignored)
221         {
222           // can't happend
223
}
224         listener.taskStopped(taskItf);
225         return;
226       }
227
228       // sanity check.
229
if (thread == null)
230       {
231         Error.bug(logger, new IllegalStateException JavaDoc(
232             "Thread component is in start state, but has no thread"));
233       }
234
235       executing = false;
236       logger.log(BasicLevel.DEBUG, "Interrupting the thread task");
237       taskStoppedListeners.add(listener);
238       thread.interrupt();
239     }
240   }
241 }
Popular Tags