|
1 | 1 | packagenet.sf.j2s.ajax; |
2 | 2 |
|
3 | | -importjava.util.concurrent.AbstractExecutorService; |
4 | | -importjava.util.concurrent.SynchronousQueue; |
5 | | -importjava.util.concurrent.ThreadFactory; |
6 | | -importjava.util.concurrent.ThreadPoolExecutor; |
7 | 3 | importjava.util.concurrent.TimeUnit; |
8 | | -importjava.util.concurrent.atomic.AtomicInteger; |
9 | 4 |
|
10 | 5 | publicclassSimpleThreadHelper { |
11 | 6 |
|
12 | | -privatestaticclassNamedThreadFactoryimplementsThreadFactory { |
13 | | -finalThreadGroupgroup; |
14 | | -finalAtomicIntegerthreadNumber =newAtomicInteger(1); |
15 | | -finalStringnamePrefix; |
16 | | - |
17 | | -publicNamedThreadFactory(Stringprefix) { |
18 | | -SecurityManagers =System.getSecurityManager(); |
19 | | -group = (s !=null)?s.getThreadGroup() : |
20 | | -Thread.currentThread().getThreadGroup(); |
21 | | -namePrefix =prefix +"-"; |
22 | | - } |
23 | | - |
24 | | -publicThreadnewThread(Runnabler) { |
25 | | -Threadt =newThread(group,r, |
26 | | -namePrefix +threadNumber.getAndIncrement(), |
27 | | -0); |
28 | | -t.setDaemon(true); |
29 | | -if (t.getPriority() !=Thread.NORM_PRIORITY) |
30 | | -t.setPriority(Thread.NORM_PRIORITY); |
31 | | -returnt; |
32 | | - } |
33 | | - |
34 | | -} |
35 | | - |
36 | | -privatestaticAbstractExecutorServicepoolExecutor; |
| 7 | +privatestaticSimpleThreadPoolExecutorpoolExecutor; |
37 | 8 |
|
38 | 9 | privatestaticbooleanpoolInitialized =false; |
39 | 10 |
|
| 11 | +privatestaticintlastCoreThreads =SimpleThreadConfig.simpleCoreThreads; |
| 12 | +privatestaticintlastMaxThreads =SimpleThreadConfig.simpleMaxThreads; |
| 13 | +privatestaticintlastIdleThreads =SimpleThreadConfig.simpleIdleThreads; |
| 14 | +privatestaticintlastQueueTasks =SimpleThreadConfig.simpleQueueTasks; |
| 15 | +privatestaticlonglastIdleSeconds =SimpleThreadConfig.simpleThreadIdleSeconds; |
| 16 | +privatestaticbooleanlastTimeout =SimpleThreadConfig.simpleThreadsTimeout; |
| 17 | + |
40 | 18 | publicstaticvoidinitializePool() { |
41 | 19 | if (poolInitialized) { |
42 | 20 | return; |
43 | 21 | } |
44 | | -poolExecutor =newThreadPoolExecutor(SimpleThreadConfig.simpleCoreThreads, |
45 | | -SimpleThreadConfig.simpleMaxThreads <=0 ?Integer.MAX_VALUE :SimpleThreadConfig.simpleMaxThreads, |
46 | | -SimpleThreadConfig.simpleThreadIdleSeconds,TimeUnit.SECONDS, |
47 | | -newSynchronousQueue<Runnable>(), |
48 | | -newNamedThreadFactory("Simple Worker")); |
49 | | -poolInitialized =true; |
| 22 | +synchronized (SimpleThreadHelper.class) { |
| 23 | +if (poolInitialized) { |
| 24 | +return; |
| 25 | +} |
| 26 | +lastCoreThreads =SimpleThreadConfig.simpleCoreThreads; |
| 27 | +lastMaxThreads =SimpleThreadConfig.simpleMaxThreads; |
| 28 | +lastIdleThreads =SimpleThreadConfig.simpleIdleThreads; |
| 29 | +lastQueueTasks =SimpleThreadConfig.simpleQueueTasks; |
| 30 | +lastIdleSeconds =SimpleThreadConfig.simpleThreadIdleSeconds; |
| 31 | +lastTimeout =SimpleThreadConfig.simpleThreadsTimeout; |
| 32 | +poolExecutor =newSimpleThreadPoolExecutor(lastCoreThreads, |
| 33 | +lastMaxThreads <=0 ?Integer.MAX_VALUE :lastMaxThreads, |
| 34 | +lastIdleThreads <=0 ?0 :lastIdleThreads, |
| 35 | +lastIdleSeconds,TimeUnit.SECONDS, |
| 36 | +lastQueueTasks <=0 ?1 :lastQueueTasks, |
| 37 | +"Simple Worker"); |
| 38 | +poolExecutor.allowCoreThreadTimeOut(lastTimeout); |
| 39 | +poolInitialized =true; |
| 40 | +} |
| 41 | +} |
| 42 | + |
| 43 | +publicstaticvoidupdatePoolConfigurations() { |
| 44 | +if (!poolInitialized ||poolExecutor ==null) { |
| 45 | +return; |
| 46 | +} |
| 47 | +intcorePoolSize =SimpleThreadConfig.simpleCoreThreads; |
| 48 | +intmaxPoolSize =SimpleThreadConfig.simpleMaxThreads; |
| 49 | +intidlePoolSize =SimpleThreadConfig.simpleIdleThreads; |
| 50 | +intmaxQueueSize =SimpleThreadConfig.simpleQueueTasks; |
| 51 | +longkeepAliveTime =SimpleThreadConfig.simpleThreadIdleSeconds; |
| 52 | +booleantimeout =SimpleThreadConfig.simpleThreadsTimeout; |
| 53 | +if (lastCoreThreads !=corePoolSize) { |
| 54 | +poolExecutor.setCorePoolSize(corePoolSize); |
| 55 | +} |
| 56 | +if (lastMaxThreads !=maxPoolSize) { |
| 57 | +poolExecutor.setMaximumPoolSize(maxPoolSize); |
| 58 | +} |
| 59 | +if (lastIdleThreads !=idlePoolSize) { |
| 60 | +poolExecutor.setIdlePoolSize(idlePoolSize); |
| 61 | +} |
| 62 | +if (lastQueueTasks !=maxQueueSize) { |
| 63 | +poolExecutor.setQueueSize(maxQueueSize); |
| 64 | +} |
| 65 | +if (lastIdleSeconds !=keepAliveTime) { |
| 66 | +poolExecutor.setKeepAliveTime(keepAliveTime,TimeUnit.SECONDS); |
| 67 | +} |
| 68 | +if (lastTimeout !=timeout) { |
| 69 | +poolExecutor.allowCoreThreadTimeOut(timeout); |
| 70 | +} |
| 71 | +lastCoreThreads =corePoolSize; |
| 72 | +lastMaxThreads =maxPoolSize; |
| 73 | +lastIdleThreads =idlePoolSize; |
| 74 | +lastQueueTasks =maxQueueSize; |
| 75 | +lastIdleSeconds =keepAliveTime; |
| 76 | +lastTimeout =timeout; |
50 | 77 | } |
51 | 78 |
|
52 | 79 | publicstaticvoidrunTask(Runnabler,Stringname) { |
|