Skip to content

Commit c53ee51

Browse files
author
zhourenjian
committed
Use a managed thread to kill pipes, avoiding creating too many threads when lots of pipes are expired
1 parent d30fe5c commit c53ee51

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

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

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
import java.util.List;
1818
import java.util.Map;
1919
import java.util.Vector;
20+
import java.util.concurrent.BlockingQueue;
21+
import java.util.concurrent.LinkedBlockingQueue;
22+
import java.util.concurrent.TimeUnit;
2023

2124
import net.sf.j2s.ajax.SimpleSerializable;
2225
import net.sf.j2s.annotation.J2SIgnore;
@@ -50,6 +53,9 @@ public static interface IPipeClosing {
5053

5154
static Map<String, SimplePipeRunnable> pipes;
5255

56+
@J2SIgnore
57+
private static BlockingQueue<SimplePipeRunnable> toBeDestroyedPipes = new LinkedBlockingQueue<SimplePipeRunnable>();
58+
5359
@J2SIgnore
5460
private SimplePipeHelper() {
5561
//
@@ -417,9 +423,41 @@ private static void monitoringAllPipes() {
417423
}
418424
System.err.println("Pipe sessions are null or empty! Pipe session monitor exited!");
419425
}
420-
426+
427+
@J2SIgnore
428+
private static void killingPipes() {
429+
while (true) {
430+
SimplePipeRunnable pipe = null;
431+
try {
432+
pipe = toBeDestroyedPipes.poll(30, TimeUnit.SECONDS);
433+
} catch (InterruptedException e1) {
434+
e1.printStackTrace();
435+
}
436+
if (pipe != null) {
437+
//System.out.println("Killing pipe " + pipe + " :// " + System.currentTimeMillis());
438+
try {
439+
if (pipe.closer != null) {
440+
pipe.closer.helpClosing(pipe); // will call pipe.pipeDestroy()
441+
} else {
442+
pipe.pipeClosed(); // will call pipe.pipeDestroy()
443+
}
444+
} catch (Throwable e) {
445+
e.printStackTrace();
446+
}
447+
//System.out.println("Killed pipe " + pipe + " :// " + System.currentTimeMillis());
448+
} else {
449+
//System.out.println("?? Empty loop?");
450+
}
451+
}
452+
}
453+
421454
@J2SIgnore
422455
static void asyncDestroyPipe(final SimplePipeRunnable pipe) {
456+
//System.out.println("To destroy pipe " + pipe);
457+
if (!toBeDestroyedPipes.contains(pipe)) {
458+
toBeDestroyedPipes.offer(pipe);
459+
}
460+
/*
423461
(new Thread("Destroy Pipe Thread") {
424462
@Override
425463
public void run() {
@@ -434,6 +472,7 @@ public void run() {
434472
}
435473
}
436474
}).start();
475+
// */
437476
}
438477

439478
@J2SIgnore
@@ -451,6 +490,13 @@ public void run() {
451490
};
452491
thread.setDaemon(true);
453492
thread.start();
493+
Thread killerThread = new Thread("Managed Pipe Session Killer") {
494+
public void run() {
495+
killingPipes();
496+
}
497+
};
498+
killerThread.setDaemon(true);
499+
killerThread.start();
454500
}
455501

456502
}

0 commit comments

Comments
 (0)