Skip to content

Commit 019d26b

Browse files
author
zhourenjian@gmail.com
committed
Use ThreadUtils to manage all threads inside J2S AJAX
1 parent 2008535 commit 019d26b

File tree

10 files changed

+91
-54
lines changed

10 files changed

+91
-54
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ protected AClass() {
6262
* }, false, true);
6363
*/
6464
public static void load(final String clazzName, final Runnable afterLoaded) {
65-
(new Thread() {
65+
ThreadUtils.runTask(new Runnable() {
6666
public void run() {
6767
try {
6868
Class<?> clz = Class.forName(clazzName);
@@ -76,6 +76,6 @@ public void run() {
7676
}
7777
if (afterLoaded != null) afterLoaded.run();
7878
}
79-
}).start();
79+
});
8080
}
8181
}

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ public static interface IXHRReceiving {
140140
protected int readyState;
141141

142142
protected String responseText;
143+
protected byte[] responseBytes;
143144
protected Document responseXML;
144145
protected IXHRCallback onreadystatechange;
145146
//private boolean overrideMimeType;
@@ -177,6 +178,14 @@ public int getReadyState() {
177178
public String getResponseText() {
178179
return responseText;
179180
}
181+
/**
182+
* Return response raw bytes of XMLHttpRequest
183+
* @return byte[] response bytes. May be null if the request is not sent
184+
* or an error happens.
185+
*/
186+
public byte[] getResponseBytes() {
187+
return responseBytes;
188+
}
180189
/**
181190
* Return the parsed XML document of the response of XMLHttpRequest.
182191
* @return Document XML document. May be null if the response text is not
@@ -194,7 +203,7 @@ public Document getResponseXML() {
194203
dbf.setAttribute("http://xml.org/sax/features/namespaces", Boolean.TRUE);
195204
try {
196205
DocumentBuilder db = dbf.newDocumentBuilder();
197-
ByteArrayInputStream biStream = new ByteArrayInputStream(responseText.getBytes());
206+
ByteArrayInputStream biStream = new ByteArrayInputStream(responseText.getBytes("utf-8"));
198207
responseXML = db.parse(biStream);
199208
} catch (Exception e) {
200209
e.printStackTrace();
@@ -351,6 +360,7 @@ public void open(String method, String url, boolean async, String user, String p
351360
this.user = user;
352361
this.password = password;
353362
responseText = null;
363+
responseBytes = null;
354364
responseXML = null;
355365
readyState = 1;
356366
status = 200; // default OK
@@ -374,13 +384,13 @@ public void send() {
374384
public void send(String str) {
375385
content = str;
376386
if (asynchronous) {
377-
(new Thread("Java2Script HTTP Request") {
387+
ThreadUtils.runTask(new Runnable() {
378388
public void run() {
379389
if (!toAbort) {
380390
request();
381391
}
382392
}
383-
}).start();
393+
}, "Java2Script HTTP Request", false);
384394
} else {
385395
request();
386396
}
@@ -518,6 +528,7 @@ private void request() {
518528
is.close();
519529
activeIS = null;
520530
responseText = null;
531+
responseBytes = baos.toByteArray();
521532
String type = connection.getHeaderField("Content-Type");
522533
if (type != null) {
523534
String charset = null;
@@ -580,7 +591,7 @@ private void request() {
580591
*/
581592
} catch (Exception e) {
582593
if (checkAbort()) return; // exception caused by abort action
583-
e.printStackTrace();
594+
//e.printStackTrace();
584595
readyState = 4;
585596
if (onreadystatechange != null) {
586597
onreadystatechange.onLoaded();
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package net.sf.j2s.ajax;
2+
3+
import java.util.concurrent.AbstractExecutorService;
4+
5+
import net.sf.j2s.annotation.J2SIgnore;
6+
7+
public class ThreadUtils {
8+
9+
private static AbstractExecutorService aes;
10+
11+
@J2SIgnore
12+
public static void setExecutorService(AbstractExecutorService s) {
13+
aes = s;
14+
}
15+
16+
@J2SIgnore
17+
public static void runTask(Runnable r, String name, boolean daemon) {
18+
if (aes == null || daemon) {
19+
Thread thread = new Thread(r, name);
20+
thread.setDaemon(daemon);
21+
thread.start();
22+
} else {
23+
aes.execute(r);
24+
}
25+
}
26+
27+
@J2SIgnore
28+
public static void runTask(Runnable r) {
29+
if (aes == null) {
30+
Thread thread = new Thread(r);
31+
thread.start();
32+
} else {
33+
aes.execute(r);
34+
}
35+
}
36+
37+
}

sources/net.sf.j2s.ajax/ajaxpipe/net/sf/j2s/ajax/SimplePipeHelper.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ static void asyncDestroyPipe(final SimplePipeRunnable pipe) {
458458
toBeDestroyedPipes.offer(pipe);
459459
}
460460
/*
461-
(new Thread("Destroy Pipe Thread") {
461+
ThreadUtils.runTask(new Runnable() {
462462
@Override
463463
public void run() {
464464
try {
@@ -471,7 +471,7 @@ public void run() {
471471
e.printStackTrace();
472472
}
473473
}
474-
}).start();
474+
}, "Destroy Pipe Thread", false);
475475
// */
476476
}
477477

@@ -483,20 +483,16 @@ static void monitoringPipe(SimplePipeRunnable pipe) {
483483
return;
484484
}
485485
monitored = true;
486-
Thread thread = new Thread("Managed Pipe Session Monitor") {
486+
ThreadUtils.runTask(new Runnable() {
487487
public void run() {
488488
monitoringAllPipes();
489489
}
490-
};
491-
thread.setDaemon(true);
492-
thread.start();
493-
Thread killerThread = new Thread("Managed Pipe Session Killer") {
490+
}, "Managed Pipe Session Monitor", true);
491+
ThreadUtils.runTask(new Runnable() {
494492
public void run() {
495493
killingPipes();
496494
}
497-
};
498-
killerThread.setDaemon(true);
499-
killerThread.start();
495+
}, "Managed Pipe Session Killer", true);
500496
}
501497

502498
}

sources/net.sf.j2s.ajax/ajaxpipe/net/sf/j2s/ajax/SimplePipeRequest.java

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ protected static void sendRequest(HttpRequest request, String method, String url
189189
public static void pipe(final SimplePipeRunnable runnable) {
190190
runnable.ajaxIn();
191191
if (getRequstMode() == MODE_LOCAL_JAVA_THREAD) {
192-
(new Thread("Pipe Request Thread") {
192+
ThreadUtils.runTask(new Runnable() {
193193
public void run() {
194194
try {
195195
runnable.ajaxRun();
@@ -201,7 +201,7 @@ public void run() {
201201
keepPipeLive(runnable);
202202
runnable.ajaxOut();
203203
}
204-
}).start();
204+
}, "Pipe Request Thread", false);
205205
} else {
206206
pipeRequest(runnable);
207207
}
@@ -213,8 +213,8 @@ public void run() {
213213
@J2SIgnore
214214
static void keepPipeLive(final SimplePipeRunnable runnable) {
215215
runnable.updateStatus(true);
216-
//if (true) return;
217-
Thread thread = new Thread(new Runnable() {
216+
/*
217+
ThreadUtils.runTask(new Runnable() {
218218
219219
public void run() {
220220
long lastLiveDetected = System.currentTimeMillis();
@@ -279,9 +279,8 @@ public void run() {
279279
} while (true);
280280
}
281281
282-
}, "Pipe Live Notifier Thread");
283-
thread.setDaemon(true);
284-
thread.start();
282+
}, "Pipe Live Notifier Thread", true);
283+
// */
285284
}
286285

287286
/**
@@ -974,11 +973,11 @@ public static String parseReceived(final String string) {
974973
if (ss == null || !ss.deserialize(string, end)) {
975974
break;
976975
}
977-
if (ss != SimpleSerializable.UNKNOWN) {
978-
String key = string.substring(start, end);
979-
SimplePipeRunnable runnable = SimplePipeHelper.getPipe(key);
980-
if (runnable != null) { // should always satisfy this condition
981-
runnable.lastPipeDataReceived = System.currentTimeMillis();
976+
String key = string.substring(start, end);
977+
SimplePipeRunnable runnable = SimplePipeHelper.getPipe(key);
978+
if (runnable != null) { // should always satisfy this condition
979+
runnable.lastPipeDataReceived = System.currentTimeMillis();
980+
if (ss != SimpleSerializable.UNKNOWN) {
982981
runnable.deal(ss);
983982
}
984983
}
@@ -1073,11 +1072,11 @@ static void ajaxPipe(final SimplePipeRunnable runnable) {
10731072
*/
10741073
{
10751074
//pipeQuery(runnable, "continuum");
1076-
(new Thread(){
1075+
ThreadUtils.runTask(new Runnable(){
10771076
public void run() {
10781077
pipeContinuum(runnable);
10791078
}
1080-
}).start();
1079+
});
10811080
} else
10821081
/**
10831082
* @j2sNative
@@ -1128,7 +1127,7 @@ public void run() {
11281127
{
11291128
final String key = runnable.pipeKey;
11301129
final long created = System.currentTimeMillis();
1131-
Thread thread = new Thread("Pipe Monitor Thread") {
1130+
ThreadUtils.runTask(new Runnable() {
11321131
public void run() {
11331132
SimplePipeRunnable runnable = null;
11341133
while ((runnable = SimplePipeHelper.getPipe(key)) != null) {
@@ -1154,9 +1153,7 @@ public void run() {
11541153
}
11551154
}
11561155
}
1157-
};
1158-
thread.setDaemon(true);
1159-
thread.start();
1156+
}, "Pipe Monitor Thread", true);
11601157
}
11611158
}
11621159

sources/net.sf.j2s.ajax/ajaxpipe/net/sf/j2s/ajax/SimplePipeRunnable.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ protected void pipeMonitoring() {
209209
SimplePipeHelper.monitoringPipe(this);
210210
return;
211211
}
212-
Thread thread = new Thread("Pipe Monitor") {
212+
ThreadUtils.runTask(new Runnable() {
213213

214214
public void run() {
215215
long interval = pipeMonitoringInterval();
@@ -238,9 +238,7 @@ public void run() {
238238
}
239239
}
240240

241-
};
242-
thread.setDaemon(true);
243-
thread.start();
241+
}, "Pipe Monitor", true);
244242
}
245243

246244
/**

sources/net.sf.j2s.ajax/ajaxrpc/net/sf/j2s/ajax/SimpleRPCRequest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public static void switchToLocalJavaThreadMode() {
7575
public static void request(final SimpleRPCRunnable runnable) {
7676
runnable.ajaxIn();
7777
if (runningMode == MODE_LOCAL_JAVA_THREAD) {
78-
(new Thread("Simple RPC Request") {
78+
ThreadUtils.runTask(new Runnable() {
7979
public void run() {
8080
try {
8181
runnable.ajaxRun();
@@ -86,7 +86,7 @@ public void run() {
8686
}
8787
runnable.ajaxOut();
8888
}
89-
}).start();
89+
}, "Simple RPC Request", false);
9090
} else {
9191
ajaxRequest(runnable);
9292
}

sources/net.sf.j2s.ajax/ajaxswt/net/sf/j2s/ajax/AWindowDelegate.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public void widgetDisposed(DisposeEvent e) {
8989
* @j2sNative
9090
*/
9191
{
92-
(new Thread() {
92+
ThreadUtils.runTask(new Runnable() {
9393
public void run() {
9494
while (win.getShell() == null) {
9595
try {
@@ -109,7 +109,7 @@ public synchronized void widgetDisposed(DisposeEvent e) {
109109
}
110110
});
111111
}
112-
}).start();
112+
});
113113
win.open();
114114
}
115115
}

sources/net.sf.j2s.ajax/ajaxswt/net/sf/j2s/ajax/SimplePipeSWTRequest.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public void run() {
5050
}
5151

5252
});
53-
(new Thread("Simple Pipe RPC Request") {
53+
ThreadUtils.runTask(new Runnable() {
5454
public void run() {
5555
try {
5656
runnable.ajaxRun();
@@ -73,7 +73,7 @@ public void run() {
7373
});
7474
} // else ?
7575
}
76-
}).start();
76+
}, "Simple Pipe RPC Request", false);
7777

7878
//SimpleRPCSWTRequest.swtRequest(runnable);
7979
} else {
@@ -88,7 +88,7 @@ public void run() {
8888
*/
8989
@J2SIgnore
9090
static void swtKeepPipeLive(final SimplePipeRunnable runnable, final Display disp) {
91-
Thread thread = new Thread("Pipe Live Notifier Thread") {
91+
ThreadUtils.runTask(new Runnable() {
9292

9393
public void run() {
9494
long lastLiveDetected = System.currentTimeMillis();
@@ -158,9 +158,7 @@ public void run() {
158158
} while (true);
159159
}
160160

161-
};
162-
thread.setDaemon(true);
163-
thread.start();
161+
}, "Pipe Live Notifier Thread", true);
164162
}
165163

166164
@J2SIgnore
@@ -190,14 +188,14 @@ public void swtOnLoaded() {
190188
SimplePipeHelper.registerPipe(runnable.pipeKey, runnable);
191189

192190
if (getPipeMode() == MODE_PIPE_CONTINUUM) {
193-
(new Thread(){
191+
ThreadUtils.runTask(new Runnable(){
194192
public void run() {
195193
swtPipeContinuum(runnable);
196194
}
197-
}).start();
195+
});
198196
} else {
199197
final String key = runnable.pipeKey;
200-
(new Thread("Pipe Monitor Thread") {
198+
ThreadUtils.runTask(new Runnable() {
201199
public void run() {
202200
SimplePipeRunnable runnable = null;
203201
while ((runnable = SimplePipeHelper.getPipe(key)) != null) {
@@ -209,7 +207,7 @@ public void run() {
209207
}
210208
}
211209
}
212-
}).start();
210+
}, "Pipe Monitor Thread", false);
213211
}
214212

215213
}

0 commit comments

Comments
 (0)