1 24 25 package org.objectweb.dream.control.activity.task.thread; 26 27 import java.util.ArrayList ; 28 import java.util.Iterator ; 29 import java.util.List ; 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 45 public class ThreadTask extends AbstractThreadTask 46 implements 47 Runnable , 48 TaskLifeCycleController 49 { 50 protected Object threadTaskLock = new Object (); 51 protected Thread thread = null; 52 protected List taskStoppedListeners = new ArrayList (); 53 54 protected boolean executing; 55 56 60 63 protected boolean isExecuting() 64 { 65 return executing; 66 } 67 68 71 protected void setExecuting(boolean b) 72 { 73 executing = b; 74 } 75 76 80 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 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 } 106 Task taskItf = null; 107 try 108 { 109 taskItf = (Task) weaveableC.getFcInterface("task"); 110 } 111 catch (NoSuchInterfaceException ignored) 112 { 113 } 115 Iterator 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 super.stopFc(); 126 } 127 catch (IllegalLifeCycleException e1) 128 { 129 } 131 logger.log(BasicLevel.DEBUG, "End of the run method"); 132 } 133 } 134 } 135 136 140 143 public void startFc() throws IllegalLifeCycleException 144 { 145 synchronized (threadTaskLock) 146 { 147 if (getFcState() == STARTED) 148 { 149 return; 150 } 151 152 if (thread != null) 154 { 155 Error.bug(logger, new IllegalStateException ( 156 "Thread component is not in start state, but has a thread")); 157 } 158 159 super.startFc(); 160 executing = true; 161 thread = new Thread (this); 162 logger.log(BasicLevel.DEBUG, "Starting the thread task"); 163 thread.start(); 164 } 165 } 166 167 170 public void stopFc() throws IllegalLifeCycleException 171 { 172 Thread t; 173 synchronized (threadTaskLock) 174 { 175 if (getFcState() == STOPPED) 176 { 177 return; 178 } 179 180 if (thread == null) 182 { 183 Error.bug(logger, new IllegalStateException ( 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 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 208 public void asyncStop(TaskStoppedListener listener) 209 { 210 synchronized (threadTaskLock) 211 { 212 if (getFcState() == STOPPED) 213 { 214 Task taskItf = null; 216 try 217 { 218 taskItf = (Task) weaveableC.getFcInterface("task"); 219 } 220 catch (NoSuchInterfaceException ignored) 221 { 222 } 224 listener.taskStopped(taskItf); 225 return; 226 } 227 228 if (thread == null) 230 { 231 Error.bug(logger, new IllegalStateException ( 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 |