1 9 package javolution.realtime; 10 11 import javolution.Configuration; 12 import javolution.lang.Reflection; 13 14 37 public final class ConcurrentThread extends Thread { 38 39 42 public static final int MAX = Configuration.concurrency(); 43 44 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 59 private ConcurrentContext _concurrentContext; 60 61 64 int logicsLength; 65 66 69 private final String _name; 70 71 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 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 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; } 132 } 133 } 134 } 135 136 139 public String toString() { 140 return _name; 141 } 142 } | Popular Tags |