Skip to content

Commit 672d863

Browse files
author
zhourenjian@gmail.com
committed
Add SimpleThreadPoolExcutor (Base on JDK 1.6)
Performance improvements: using default charset encoding instead of 8859-1 encoding Throws exception on invalid WLL format Other improvements
1 parent 75b2e49 commit 672d863

File tree

9 files changed

+574
-252
lines changed

9 files changed

+574
-252
lines changed

sources/net.sf.j2s.ajax/ajaxcore/net/sf/j2s/ajax/HttpRequest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,9 @@ public String getResponseText() {
188188
return responseText;
189189
}
190190
ByteArrayOutputStream baos = responseBAOS;
191+
if (baos == null) {
192+
return null;
193+
}
191194
String type = responseType;
192195
if (type != null) {
193196
String charset = null;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2010 - 2011 webuzz.im
3+
*
4+
* Author:
5+
* Zhou Renjian / zhourenjian@gmail.com - initial API and implementation
6+
*******************************************************************************/
7+
8+
package net.sf.j2s.ajax;
9+
10+
import java.util.concurrent.atomic.AtomicInteger;
11+
import java.util.concurrent.ThreadFactory;
12+
13+
/**
14+
* A Thread factory so we can tell what are those threads for.
15+
*
16+
* @author zhourenjian
17+
*
18+
*/
19+
public class SimpleNamedThreadFactory implements ThreadFactory {
20+
final ThreadGroup group;
21+
final AtomicInteger threadNumber = new AtomicInteger(1);
22+
final String namePrefix;
23+
24+
public SimpleNamedThreadFactory(String prefix) {
25+
SecurityManager s = System.getSecurityManager();
26+
group = (s != null)? s.getThreadGroup() :
27+
Thread.currentThread().getThreadGroup();
28+
namePrefix = prefix + "-";
29+
}
30+
31+
public Thread newThread(Runnable r) {
32+
Thread t = new Thread(group, r,
33+
namePrefix + threadNumber.getAndIncrement(),
34+
0);
35+
if (t.getPriority() != Thread.NORM_PRIORITY)
36+
t.setPriority(Thread.NORM_PRIORITY);
37+
return t;
38+
}
39+
40+
}
Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package net.sf.j2s.ajax;
22

3+
import java.util.Properties;
4+
35
public class SimpleThreadConfig {
46
/**
57
* Core thread number. Core threads will be kept in thread pool to
@@ -9,14 +11,35 @@ public class SimpleThreadConfig {
911

1012
/**
1113
* Max thread number. Server will allow this number of threads at the
12-
* peak. Default to 128. If set to -1, if there is no limit.
14+
* peak. Default number is 128. If set to -1, if there is no limit.
1315
*/
1416
public static int simpleMaxThreads = 128;
15-
17+
18+
/**
19+
* Idle thread number. Server will keep this number of idle threads if
20+
* possible. Default number is 10.
21+
*/
22+
public static int simpleIdleThreads = 10;
23+
1624
/**
1725
* If a thread is idle for given seconds, and thread number is greater
1826
* than simpleMaxThreads, this thread will be recycled.
1927
*/
20-
public static long simpleThreadIdleSeconds = 120L;
28+
public static long simpleThreadIdleSeconds = 60L;
29+
30+
/**
31+
* Allow threads to time out or not.
32+
*/
33+
public static boolean simpleThreadsTimeout = false;
34+
35+
/**
36+
* Queue task number. Server will keep this number of tasks waiting in
37+
* Queue. Default is 100.
38+
*/
39+
public static int simpleQueueTasks = 100;
2140

41+
public static void update(Properties props) {
42+
SimpleThreadHelper.updatePoolConfigurations();
43+
}
44+
2245
}

sources/net.sf.j2s.ajax/ajaxcore/net/sf/j2s/ajax/SimpleThreadHelper.java

Lines changed: 63 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,79 @@
11
package net.sf.j2s.ajax;
22

3-
import java.util.concurrent.AbstractExecutorService;
4-
import java.util.concurrent.SynchronousQueue;
5-
import java.util.concurrent.ThreadFactory;
6-
import java.util.concurrent.ThreadPoolExecutor;
73
import java.util.concurrent.TimeUnit;
8-
import java.util.concurrent.atomic.AtomicInteger;
94

105
public class SimpleThreadHelper {
116

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;
378

389
private static boolean poolInitialized = false;
3910

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+
4018
public static void initializePool() {
4119
if (poolInitialized) {
4220
return;
4321
}
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;
5077
}
5178

5279
public static void runTask(Runnable r, String name) {

0 commit comments

Comments
 (0)