Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit672d863

Browse files
author
zhourenjian@gmail.com
committed
Add SimpleThreadPoolExcutor (Base on JDK 1.6)
Performance improvements: using default charset encoding instead of 8859-1 encodingThrows exception on invalid WLL formatOther improvements
1 parent75b2e49 commit672d863

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
returnresponseText;
189189
}
190190
ByteArrayOutputStreambaos =responseBAOS;
191+
if (baos ==null) {
192+
returnnull;
193+
}
191194
Stringtype =responseType;
192195
if (type !=null) {
193196
Stringcharset =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+
packagenet.sf.j2s.ajax;
9+
10+
importjava.util.concurrent.atomic.AtomicInteger;
11+
importjava.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+
publicclassSimpleNamedThreadFactoryimplementsThreadFactory {
20+
finalThreadGroupgroup;
21+
finalAtomicIntegerthreadNumber =newAtomicInteger(1);
22+
finalStringnamePrefix;
23+
24+
publicSimpleNamedThreadFactory(Stringprefix) {
25+
SecurityManagers =System.getSecurityManager();
26+
group = (s !=null)?s.getThreadGroup() :
27+
Thread.currentThread().getThreadGroup();
28+
namePrefix =prefix +"-";
29+
}
30+
31+
publicThreadnewThread(Runnabler) {
32+
Threadt =newThread(group,r,
33+
namePrefix +threadNumber.getAndIncrement(),
34+
0);
35+
if (t.getPriority() !=Thread.NORM_PRIORITY)
36+
t.setPriority(Thread.NORM_PRIORITY);
37+
returnt;
38+
}
39+
40+
}
Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
packagenet.sf.j2s.ajax;
22

3+
importjava.util.Properties;
4+
35
publicclassSimpleThreadConfig {
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. Defaultto 128. If set to -1, if there is no limit.
14+
* peak. Defaultnumber is 128. If set to -1, if there is no limit.
1315
*/
1416
publicstaticintsimpleMaxThreads =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+
publicstaticintsimpleIdleThreads =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-
publicstaticlongsimpleThreadIdleSeconds =120L;
28+
publicstaticlongsimpleThreadIdleSeconds =60L;
29+
30+
/**
31+
* Allow threads to time out or not.
32+
*/
33+
publicstaticbooleansimpleThreadsTimeout =false;
34+
35+
/**
36+
* Queue task number. Server will keep this number of tasks waiting in
37+
* Queue. Default is 100.
38+
*/
39+
publicstaticintsimpleQueueTasks =100;
2140

41+
publicstaticvoidupdate(Propertiesprops) {
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
packagenet.sf.j2s.ajax;
22

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

105
publicclassSimpleThreadHelper {
116

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

389
privatestaticbooleanpoolInitialized =false;
3910

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

5279
publicstaticvoidrunTask(Runnabler,Stringname) {

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp