KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javolution > realtime > ConcurrentThread


1 /*
2  * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
3  * Copyright (C) 2005 - 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.realtime;
10
11 import javolution.Configuration;
12 import javolution.lang.Reflection;
13
14 /**
15  * <p> This class represents "worker" threads employed by
16  * {@link ConcurrentContext} to perform concurrent
17  * {@link ConcurrentContext#execute executions} on multi-processors
18  * systems.</p>
19  *
20  * <p> Instances of this class are created at start-up and maintained
21  * on-standby in order to execute quickly.</p>
22  *
23  * <p> The default context for instances of this class is a {@link PoolContext},
24  * unlike normal threads for which the default context is
25  * a {@link HeapContext}.</p>
26  *
27  * <p> To avoid thread proliferation, the number of instance of this class
28  * is voluntarily limited (see <a HREF=
29  * "{@docRoot}/overview-summary.html#configuration">Javolution
30  * Configuration</a> for details). Using the default configuration,
31  * only systems with Hyper-Threading or multi-processors have instances
32  * of this class.</p>
33  *
34  * @author <a HREF="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
35  * @version 1.0, October 4, 2004
36  */

37 public final class ConcurrentThread extends Thread {
38
39     /**
40      * Holds the maximum number of {@link ConcurrentThread}.
41      */

42     public static final int MAX = Configuration.concurrency();
43     
44     /**
45      * Holds the concurrent threads instances.
46      */

47     private static final ConcurrentThread[] INSTANCES
48         = new ConcurrentThread[MAX];
49     static {
50         for (int i=0; i < MAX; i++) {
51             INSTANCES[i] = new ConcurrentThread(i);
52             INSTANCES[i].start();
53         }
54     }
55     
56     /**
57      * Holds the concurrent context this thread belongs to.
58      */

59     private ConcurrentContext _concurrentContext;
60
61     /**
62      * Holds the number of logics to be executed.
63      */

64     int logicsLength;
65
66     /**
67      * Holds this thread's name.
68      */

69     private final String _name;
70
71     /**
72      * Creates a new concurrent thread with the specified index.
73      *
74      * @param index the thread index in the range [0 .. {@link #MAX}[
75      */

76     private ConcurrentThread(int index) {
77         _name = "ConcurrentThread-" + index;
78         if (SET_DAEMON != null) {
79             SET_DAEMON.invoke(this, new Boolean(true));
80         }
81     }
82     private static final Reflection.Method SET_DAEMON
83         = Reflection.getMethod("java.lang.Thread.setDaemon(bool)");
84     
85
86     /**
87      * Requests for all {@link ConcurrentThread} available to execute the logics
88      * of the specified concurrent context.
89      *
90      * @return the actual number of threads working for this concurrent context.
91      */

92     static int execute(ConcurrentContext concurrentContext) {
93         int concurrency = 0;
94         if (MAX > 0) {
95             synchronized (INSTANCES) {
96                 for (int i=0; i < MAX; i++) {
97                     if (INSTANCES[i]._concurrentContext == null) {
98                         synchronized (INSTANCES[i]) {
99                             INSTANCES[i]._concurrentContext = concurrentContext;
100                             INSTANCES[i].notify();
101                         }
102                         concurrency++;
103                     }
104                 }
105             }
106         }
107         return concurrency;
108     }
109
110     /**
111      * Overrides parent's <code>run</code> method.
112      */

113     public void run() {
114         PoolContext rootContext = (PoolContext) Context.currentContext();
115         while (true) {
116            try {
117                synchronized (this) {
118                     while (_concurrentContext == null) {
119                         this.wait();
120                     }
121                }
122                rootContext.setOuter(_concurrentContext);
123                while (_concurrentContext.executeNext()) {
124                    rootContext.recyclePools();
125                }
126            } catch (Throwable e) {
127                e.printStackTrace();
128            } finally {
129                synchronized (INSTANCES) {
130                    _concurrentContext = null; // Thread available again.
131
}
132            }
133         }
134     }
135
136     /**
137      * Overrides parent's <code>toString</code> method.
138      */

139     public String toString() {
140         return _name;
141     }
142 }
Popular Tags