|
1 | 1 | package net.sf.j2s.ajax;
|
2 | 2 |
|
3 |
| -import java.util.concurrent.AbstractExecutorService; |
4 |
| -import java.util.concurrent.SynchronousQueue; |
5 |
| -import java.util.concurrent.ThreadFactory; |
6 |
| -import java.util.concurrent.ThreadPoolExecutor; |
7 | 3 | import java.util.concurrent.TimeUnit;
|
8 |
| -import java.util.concurrent.atomic.AtomicInteger; |
9 | 4 |
|
10 | 5 | public class SimpleThreadHelper {
|
11 | 6 |
|
12 |
| - private static class NamedThreadFactory implements ThreadFactory { |
13 |
| - final ThreadGroup group; |
14 |
| - final AtomicInteger threadNumber = new AtomicInteger(1); |
15 |
| - final String namePrefix; |
16 |
| - |
17 |
| - public NamedThreadFactory(String prefix) { |
18 |
| - SecurityManager s = System.getSecurityManager(); |
19 |
| - group = (s != null)? s.getThreadGroup() : |
20 |
| - Thread.currentThread().getThreadGroup(); |
21 |
| - namePrefix = prefix + "-"; |
22 |
| - } |
23 |
| - |
24 |
| - public Thread newThread(Runnable r) { |
25 |
| - Thread t = new Thread(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 |
| - return t; |
32 |
| - } |
33 |
| - |
34 |
| - } |
35 |
| - |
36 |
| - private static AbstractExecutorService poolExecutor; |
| 7 | + private static SimpleThreadPoolExecutor poolExecutor; |
37 | 8 |
|
38 | 9 | private static boolean poolInitialized = false;
|
39 | 10 |
|
| 11 | + private static int lastCoreThreads = SimpleThreadConfig.simpleCoreThreads; |
| 12 | + private static int lastMaxThreads = SimpleThreadConfig.simpleMaxThreads; |
| 13 | + private static int lastIdleThreads = SimpleThreadConfig.simpleIdleThreads; |
| 14 | + private static int lastQueueTasks = SimpleThreadConfig.simpleQueueTasks; |
| 15 | + private static long lastIdleSeconds = SimpleThreadConfig.simpleThreadIdleSeconds; |
| 16 | + private static boolean lastTimeout = SimpleThreadConfig.simpleThreadsTimeout; |
| 17 | + |
40 | 18 | public static void initializePool() {
|
41 | 19 | if (poolInitialized) {
|
42 | 20 | return;
|
43 | 21 | }
|
44 |
| - poolExecutor = new ThreadPoolExecutor(SimpleThreadConfig.simpleCoreThreads, |
45 |
| - SimpleThreadConfig.simpleMaxThreads <= 0 ? Integer.MAX_VALUE : SimpleThreadConfig.simpleMaxThreads, |
46 |
| - SimpleThreadConfig.simpleThreadIdleSeconds, TimeUnit.SECONDS, |
47 |
| - new SynchronousQueue<Runnable>(), |
48 |
| - new NamedThreadFactory("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 = new SimpleThreadPoolExecutor(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 | + public static void updatePoolConfigurations() { |
| 44 | + if (!poolInitialized || poolExecutor == null) { |
| 45 | + return; |
| 46 | + } |
| 47 | + int corePoolSize = SimpleThreadConfig.simpleCoreThreads; |
| 48 | + int maxPoolSize = SimpleThreadConfig.simpleMaxThreads; |
| 49 | + int idlePoolSize = SimpleThreadConfig.simpleIdleThreads; |
| 50 | + int maxQueueSize = SimpleThreadConfig.simpleQueueTasks; |
| 51 | + long keepAliveTime = SimpleThreadConfig.simpleThreadIdleSeconds; |
| 52 | + boolean timeout = 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 | public static void runTask(Runnable r, String name) {
|
|
0 commit comments