1 | package desmoj.core.simulator; |
2 | |
3 | import desmoj.core.exception.SimFinishedException; |
4 | |
5 | /** |
6 | * SimThreads are used to mimic coroutine behaviour with the help of native Java |
7 | * threads. SimThreads are attributes of SimProcesses only. |
8 | * |
9 | * @version DESMO-J, Ver. 2.3.3 copyright (c) 2011 |
10 | * @author Tim Lechler |
11 | * |
12 | * Licensed under the Apache License, Version 2.0 (the "License"); |
13 | * you may not use this file except in compliance with the License. You |
14 | * may obtain a copy of the License at |
15 | * http://www.apache.org/licenses/LICENSE-2.0 |
16 | * |
17 | * Unless required by applicable law or agreed to in writing, software |
18 | * distributed under the License is distributed on an "AS IS" |
19 | * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
20 | * or implied. See the License for the specific language governing |
21 | * permissions and limitations under the License. |
22 | * |
23 | */ |
24 | public class SimThread extends Thread { |
25 | |
26 | /** |
27 | * The sim-process this simthread serves for. |
28 | */ |
29 | SimProcess simProc; |
30 | |
31 | /** |
32 | * Constructs a simple SimThread for the given SimProcess object in the |
33 | * given threadgroup. The threadgroup is defined by the experiment the |
34 | * SimThread's SimProcess is associated to. For better identification and |
35 | * easier debugging, the SimThread carries the sim-process' name. |
36 | * |
37 | * @param group |
38 | * java.lang.ThreadGroup : The sim-process' threadgroup |
39 | * @param siPro |
40 | * SimProcess : The sim-process |
41 | */ |
42 | SimThread(ThreadGroup group, SimProcess siPro) { |
43 | |
44 | super(group, siPro.getName()); |
45 | simProc = siPro; |
46 | |
47 | } |
48 | |
49 | /** |
50 | * Returns the status of the sim-process wether it is still running active |
51 | * with this simthread or not. A return value of <code>true</code> indicates |
52 | * that this simthread is still alive while <code>false</code> indicates |
53 | * that the simthread is not in a blocking situation any more since it has |
54 | * exited its <code>run()</code> method. |
55 | * |
56 | * @return boolean : Is <code>true</code> if the simthread is still alive, |
57 | * <code>false</code> if the simthread has exited its |
58 | * <code>run()</code> method |
59 | */ |
60 | boolean isRunning() { |
61 | return simProc.isReady(); |
62 | } |
63 | |
64 | /** |
65 | * Method is used to stop all running threads after an experient is stopped. |
66 | * Calls the sim-process' <code>clearThread()<code> method to free the |
67 | * scheduler waiting in the block. |
68 | */ |
69 | void kill() { |
70 | |
71 | simProc.resume(); |
72 | |
73 | } |
74 | |
75 | /** |
76 | * Run starts when the associated SimProcess is activated for the first |
77 | * time. To prevent numerous simthread from running amok, they are |
78 | * immediately forced into a Java native wait situation where they can be |
79 | * released from using the method <code>activate(TimeSpan dt)</code> or |
80 | * <code>activate(TimeInstant time)</code>. |
81 | */ |
82 | public void run() { |
83 | |
84 | // let all other threads, esp. the main thread get into the block |
85 | // yield(); |
86 | // catch SimFinishedExceptions to clear this thread |
87 | // needed to get all SimProcesses cleared up after end of simulation |
88 | try { |
89 | simProc.lifeCycle(); |
90 | if (simProc.currentlySendTraceNotes()) { |
91 | simProc.sendTraceNote("terminates"); |
92 | } |
93 | } catch (SimFinishedException sfEx) { |
94 | ; // nothing done here, sfEx was just used to |
95 | // finish this simthread after end of simulation |
96 | } |
97 | |
98 | // update running status, which is now stopped concerning the model |
99 | simProc.setRunning(false); |
100 | |
101 | // update status flag for using the sim-process' simthread |
102 | simProc.setTerminated(true); |
103 | |
104 | // release the waiting scheduler |
105 | simProc.freeThread(); |
106 | |
107 | // for debugging purposes only |
108 | // System.out.println(getName()+" exits"); |
109 | } |
110 | } |