Skip to content

Commit 7008bbd

Browse files
committed
Update talk.java to use ThreadPool.
1 parent c5ae113 commit 7008bbd

File tree

3 files changed

+31
-24
lines changed

3 files changed

+31
-24
lines changed

WEB-INF/lib/struct-2.0.jar

-13.8 KB
Binary file not shown.

src/tinystruct/examples/reading.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
import org.tinystruct.dom.Element;
3939
import org.tinystruct.system.Authentication;
4040
import org.tinystruct.system.util.Base64;
41-
import org.tinystruct.system.util.URLFileLoader;
41+
import org.tinystruct.system.util.URLResourceLoader;
4242

4343
import custom.objects.User;
4444
import custom.objects.article;
@@ -583,7 +583,7 @@ public static void main(String[]args) throws ApplicationException, IOException{
583583

584584
while(p <= 47) {
585585
URL url = new URL("http://www.old-gospel.net/viewthread.php?tid=446&extra=page%3D1&page="+(p++));
586-
URLFileLoader file = new URLFileLoader(url);
586+
URLResourceLoader file = new URLResourceLoader(url);
587587
file.setCharset("gbk");
588588
String content = file.getContent().toString();
589589
Matcher m = pattern.matcher(content);

src/tinystruct/examples/talk.java

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@
2424
public class talk extends AbstractApplication {
2525

2626
private static final long TIMEOUT = 200;
27+
private static final int DEFAULT_POOL_SIZE = 3;
2728
protected final Map<String, Queue<Builder>> list = new ConcurrentHashMap<String, Queue<Builder>>();
2829
protected final Map<String, Queue<Builder>> meetings = new ConcurrentHashMap<String, Queue<Builder>>();
2930
protected final Map<String, List<String>> sessions = new ConcurrentHashMap<String, List<String>>();
30-
private final ExecutorService service = Executors.newFixedThreadPool(3);
31+
private ExecutorService service;
3132

3233
@Override
3334
public void init() {
@@ -36,23 +37,25 @@ public void init() {
3637
this.setAction("talk/version", "version");
3738
this.setAction("talk/testing", "testing");
3839

39-
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
40-
@Override
41-
public void run() {
42-
service.shutdown();
43-
while (true) {
44-
try {
45-
System.out.println("Waiting for the service to terminate...");
46-
if (service.awaitTermination(5, TimeUnit.SECONDS)) {
47-
System.out.println("Service will be terminated soon.");
40+
if (this.service != null) {
41+
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
42+
@Override
43+
public void run() {
44+
service.shutdown();
45+
while (true) {
46+
try {
47+
System.out.println("Waiting for the service to terminate...");
48+
if (service.awaitTermination(5, TimeUnit.SECONDS)) {
49+
System.out.println("Service will be terminated soon.");
4850
break;
49-
}
50-
} catch (InterruptedException e) {
51-
e.printStackTrace();
52-
}
53-
}
54-
}
55-
}));
51+
}
52+
} catch (InterruptedException e) {
53+
e.printStackTrace();
54+
}
55+
}
56+
}
57+
}));
58+
}
5659
}
5760

5861
/**
@@ -97,7 +100,7 @@ public final String save(final Object meetingCode, final Builder builder) {
97100
this.meetings.notifyAll();
98101
}
99102

100-
service.execute(new Runnable(){
103+
this.getService().execute(new Runnable(){
101104
@Override
102105
public void run() {
103106
synchronized(talk.this.meetings) {
@@ -117,6 +120,10 @@ public void run() {
117120
return builder.toString();
118121
}
119122

123+
private ExecutorService getService() {
124+
return this.service!=null? this.service : Executors.newFixedThreadPool(DEFAULT_POOL_SIZE);
125+
}
126+
120127
/**
121128
* Poll message from the messages of the session specified sessionId.
122129
* @param sessionId
@@ -197,7 +204,7 @@ public boolean testing(final int n) throws ApplicationException {
197204
sess.add("{B}");
198205
this.sessions.put("[M001]", sess);
199206

200-
service.execute(new Runnable(){
207+
this.getService().execute(new Runnable(){
201208
@Override
202209
public void run() {
203210
int i=0;
@@ -215,7 +222,7 @@ public void run() {
215222
}
216223
});
217224

218-
service.execute(new Runnable(){
225+
this.getService().execute(new Runnable(){
219226
@Override
220227
public void run() {
221228
int i=0;
@@ -233,7 +240,7 @@ public void run() {
233240
}
234241
});
235242

236-
service.execute(new Runnable(){
243+
this.getService().execute(new Runnable(){
237244
@Override
238245
public void run() {
239246
// TODO Auto-generated method stub
@@ -252,7 +259,7 @@ public void run() {
252259
}
253260
});
254261

255-
service.execute(new Runnable(){
262+
this.getService().execute(new Runnable(){
256263
@Override
257264
public void run() {
258265
// TODO Auto-generated method stub

0 commit comments

Comments
 (0)