KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javolution > context > ConcurrentThread


1 /*
2  * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
3  * Copyright (C) 2006 - Javolution (http://javolution.org/)
4  * All rights reserved.
5  *
6  * Permission to use, copy, modify, and distribute this software is
7  * freely granted, provided that this notice is preserved.
8  */

9 package javolution.context;
10
11 import j2mex.realtime.MemoryArea;
12 import j2mex.realtime.RealtimeThread;
13 import javolution.lang.Reflection;
14
15 /**
16  * <p> This class represents the default {@link ConcurrentExecutor}
17  * implementation used by {@link ConcurrentContext}. Executions
18  * are performed in the same memory area and at the same priority
19  * as the calling thread.</p>
20  *
21  * @author <a HREF="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
22  * @version 4.2, December 14, 2006
23  */

24 public class ConcurrentThread extends RealtimeThread implements
25         ConcurrentExecutor {
26
27     private volatile Runnable JavaDoc _logic;
28
29     private MemoryArea _memoryArea;
30
31     private int _priority;
32
33     private Status _status;
34
35     private boolean _terminate;
36
37     private String JavaDoc _name;
38
39     private Thread JavaDoc _source;
40
41     /**
42      * Default constructor.
43      */

44     public ConcurrentThread() {
45         _name = "ConcurrentThread-" + getCount();
46         if (SET_NAME != null) {
47             SET_NAME.invoke(this, _name);
48         }
49         if (SET_DAEMON != null) {
50             SET_DAEMON.invoke(this, new Boolean JavaDoc(true));
51         }
52     }
53
54     private synchronized int getCount() {
55         return _Count++;
56     }
57
58     private static int _Count;
59
60     private static final Reflection.Method SET_NAME = Reflection
61             .getMethod("java.lang.Thread.setName(String)");
62
63     private static final Reflection.Method SET_DAEMON = Reflection
64             .getMethod("java.lang.Thread.setDaemon(boolean)");
65
66     /**
67      * Executes the concurrent logics sequentially.
68      */

69     public void run() {
70         while (true) { // Main loop.
71
synchronized (this) { // Waits for a task.
72
try {
73                     while ((_logic == null) && !_terminate)
74                         this.wait();
75                 } catch (InterruptedException JavaDoc e) {
76                     throw new ConcurrentException(e);
77                 }
78             }
79             if (_logic == null)
80                 break; // Terminates.
81
try {
82                 Thread JavaDoc current = Thread.currentThread();
83                 if (current.getPriority() != _priority) {
84                     current.setPriority(_priority);
85                 }
86                 _status.started();
87                 _memoryArea.executeInArea(_logic);
88             } catch (Throwable JavaDoc error) {
89                 _status.error(error);
90             } finally {
91                 _status.completed();
92                 _status = null;
93                 _source = null;
94                 _logic = null; // Last (ready).
95
}
96         }
97     }
98
99     // Implements ConcurrentExecutor
100
public boolean execute(Runnable JavaDoc logic, Status status) {
101         if (_logic != null)
102             return false; // Shortcut to avoid synchronizing.
103
synchronized (this) {
104             if (_logic != null)
105                 return false; // Synchronized check.
106
_memoryArea = RealtimeThread.getCurrentMemoryArea();
107             _source = currentThread();
108             if (_source instanceof ConcurrentThread) {
109                 _source = ((ConcurrentThread) _source)._source;
110             }
111             _priority = _source.getPriority();
112             _status = status;
113             _logic = logic; // Must be last.
114
this.notify();
115             return true;
116         }
117     }
118
119     // Implements ConcurrentExecutor
120
public void terminate() {
121         synchronized (this) {
122             _terminate = true;
123             this.notify();
124         }
125     }
126
127     /**
128      * Returns the name of this concurrent thread as well as its calling source
129      * (in parenthesis).
130      *
131      * @return the string representation of this thread.
132      */

133     public String JavaDoc toString() {
134         return _name + "(" + _source + ")";
135     }
136
137 }
138
Popular Tags