Java ThreadPoolExecutor stecken, während der Verwendung ArrayBlockingQueue
Arbeite ich auf einige Anwendung und mit der ThreadPoolExecutor für den Umgang mit verschiedenen Aufgaben. ThreadPoolExecutor ist, stecken nach einiger Dauer. Um dies zu simulieren, die in einer einfacheren Umgebung, die ich geschrieben habe einen einfachen code, wo ich bin in der Lage zu simulieren, das Problem.
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class MyThreadPoolExecutor {
private int poolSize = 10;
private int maxPoolSize = 50;
private long keepAliveTime = 10;
private ThreadPoolExecutor threadPool = null;
private final ArrayBlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(
100000);
public MyThreadPoolExecutor() {
threadPool = new ThreadPoolExecutor(poolSize, maxPoolSize,
keepAliveTime, TimeUnit.SECONDS, queue);
threadPool.setRejectedExecutionHandler(new RejectedExecutionHandler() {
@Override
public void rejectedExecution(Runnable runnable,
ThreadPoolExecutor threadPoolExecutor) {
System.out
.println("Execution rejected. Please try restarting the application.");
}
});
}
public void runTask(Runnable task) {
threadPool.execute(task);
}
public void shutDown() {
threadPool.shutdownNow();
}
public ThreadPoolExecutor getThreadPool() {
return threadPool;
}
public void setThreadPool(ThreadPoolExecutor threadPool) {
this.threadPool = threadPool;
}
public static void main(String[] args) {
MyThreadPoolExecutor mtpe = new MyThreadPoolExecutor();
for (int i = 0; i < 1000; i++) {
final int j = i;
mtpe.runTask(new Runnable() {
@Override
public void run() {
System.out.println(j);
}
});
}
}
}
Führe diesen code ein paar mal. Es in der Regel print-outs die Anzahl auf der Konsole und wenn alle threads zu Ende, es existiert. Aber in Zeiten, es beendet alle tasks und dann ist nicht immer beendet. Der thread-dump ist wie folgt:
MyThreadPoolExecutor [Java Application]
MyThreadPoolExecutor at localhost:2619 (Suspended)
Daemon System Thread [Attach Listener] (Suspended)
Daemon System Thread [Signal Dispatcher] (Suspended)
Daemon System Thread [Finalizer] (Suspended)
Object.wait(long) line: not available [native method]
ReferenceQueue<T>.remove(long) line: not available
ReferenceQueue<T>.remove() line: not available
Finalizer$FinalizerThread.run() line: not available
Daemon System Thread [Reference Handler] (Suspended)
Object.wait(long) line: not available [native method]
Reference$Lock(Object).wait() line: 485
Reference$ReferenceHandler.run() line: not available
Thread [pool-1-thread-1] (Suspended)
Unsafe.park(boolean, long) line: not available [native method]
LockSupport.park(Object) line: not available
AbstractQueuedSynchronizer$ConditionObject.await() line: not available
ArrayBlockingQueue<E>.take() line: not available
ThreadPoolExecutor.getTask() line: not available
ThreadPoolExecutor$Worker.run() line: not available
Thread.run() line: not available
Thread [pool-1-thread-2] (Suspended)
Unsafe.park(boolean, long) line: not available [native method]
LockSupport.park(Object) line: not available
AbstractQueuedSynchronizer$ConditionObject.await() line: not available
ArrayBlockingQueue<E>.take() line: not available
ThreadPoolExecutor.getTask() line: not available
ThreadPoolExecutor$Worker.run() line: not available
Thread.run() line: not available
Thread [pool-1-thread-3] (Suspended)
Unsafe.park(boolean, long) line: not available [native method]
LockSupport.park(Object) line: not available
AbstractQueuedSynchronizer$ConditionObject.await() line: not available
ArrayBlockingQueue<E>.take() line: not available
ThreadPoolExecutor.getTask() line: not available
ThreadPoolExecutor$Worker.run() line: not available
Thread.run() line: not available
Thread [pool-1-thread-4] (Suspended)
Unsafe.park(boolean, long) line: not available [native method]
LockSupport.park(Object) line: not available
AbstractQueuedSynchronizer$ConditionObject.await() line: not available
ArrayBlockingQueue<E>.take() line: not available
ThreadPoolExecutor.getTask() line: not available
ThreadPoolExecutor$Worker.run() line: not available
Thread.run() line: not available
Thread [pool-1-thread-6] (Suspended)
Unsafe.park(boolean, long) line: not available [native method]
LockSupport.park(Object) line: not available
AbstractQueuedSynchronizer$ConditionObject.await() line: not available
ArrayBlockingQueue<E>.take() line: not available
ThreadPoolExecutor.getTask() line: not available
ThreadPoolExecutor$Worker.run() line: not available
Thread.run() line: not available
Thread [pool-1-thread-8] (Suspended)
Unsafe.park(boolean, long) line: not available [native method]
LockSupport.park(Object) line: not available
AbstractQueuedSynchronizer$ConditionObject.await() line: not available
ArrayBlockingQueue<E>.take() line: not available
ThreadPoolExecutor.getTask() line: not available
ThreadPoolExecutor$Worker.run() line: not available
Thread.run() line: not available
Thread [pool-1-thread-5] (Suspended)
Unsafe.park(boolean, long) line: not available [native method]
LockSupport.park(Object) line: not available
AbstractQueuedSynchronizer$ConditionObject.await() line: not available
ArrayBlockingQueue<E>.take() line: not available
ThreadPoolExecutor.getTask() line: not available
ThreadPoolExecutor$Worker.run() line: not available
Thread.run() line: not available
Thread [pool-1-thread-10] (Suspended)
Unsafe.park(boolean, long) line: not available [native method]
LockSupport.park(Object) line: not available
AbstractQueuedSynchronizer$ConditionObject.await() line: not available
ArrayBlockingQueue<E>.take() line: not available
ThreadPoolExecutor.getTask() line: not available
ThreadPoolExecutor$Worker.run() line: not available
Thread.run() line: not available
Thread [pool-1-thread-9] (Suspended)
Unsafe.park(boolean, long) line: not available [native method]
LockSupport.park(Object) line: not available
AbstractQueuedSynchronizer$ConditionObject.await() line: not available
ArrayBlockingQueue<E>.take() line: not available
ThreadPoolExecutor.getTask() line: not available
ThreadPoolExecutor$Worker.run() line: not available
Thread.run() line: not available
Thread [pool-1-thread-7] (Suspended)
Unsafe.park(boolean, long) line: not available [native method]
LockSupport.park(Object) line: not available
AbstractQueuedSynchronizer$ConditionObject.await() line: not available
ArrayBlockingQueue<E>.take() line: not available
ThreadPoolExecutor.getTask() line: not available
ThreadPoolExecutor$Worker.run() line: not available
Thread.run() line: not available
Thread [DestroyJavaVM] (Suspended)
C:\Program Files\Java\jre1.6.0_07\bin\javaw.exe (Jun 17, 2010 10:42:33 AM)
In meiner tatsächlichen Anwendung,ThreadPoolExecutor threads gehen, in diesem Zustand und reagiert dann nicht mehr.
Grüße,
Ravi Rao
- Eine Idee ist, dass Sie versuchen, ExecutorService, ein Favorit von mir.
- Andren, Einen ThreadPoolExecutor ist ein ExecutorService. ExecutorService ist einfach eine Schnittstelle. In der Java 1.5-Bibliothek, ThreadPoolExecutor ist die einzige direkte Implementierungen von ExecutorService-Schnittstelle. Es ist ein AbstractExecutorService und ein DelegatedExecutorService, die nicht stand-alone-funktionale Klassen. Da gibt es auch eine SheceduledExecutorService-Schnittstelle, die erweitert ExecutorService und hat einen einzigen, konkreten Umsetzung.
Du musst angemeldet sein, um einen Kommentar abzugeben.
In Ihrem
main
Methode, Sie nie rufen Siemtpe.shutdown()
. Ein ThreadPoolExecutor wird versuchen, zu halten IhrcorePoolSize
lebendig auf unbestimmte Zeit. Manchmal hat man Glück und Sie haben mehr alscorePoolSize
threads am Leben, so dass jeder worker-thread wechselt in einen bedingte Logik-Zweig, der es ermöglicht, zu kündigen, nachdem Sie Ihre angegebene timeout-Zeit von 10 Sekunden. Aber, wie Sie bemerkt haben, manchmal ist dies nicht der Fall, also ist jeder thread in dem Testamentsvollstrecker wird der block auf ArrayBlockingQueue.nehmen() und warten auf eine neue Aufgabe.Bitte beachten Sie, es gibt einen signifikanten Unterschied zwischen ExecutorService.shutdown() und ExecutorService.shutdownNow(). Wenn Sie anrufen ExecutorService.shutdownNow() als wrapper-Umsetzung gibt, werden Sie gelegentlich fallen einige Aufgaben, die nicht zugewiesen wurden, für die Ausführung.
Update: Da meine ursprüngliche Antwort, die
ThreadPoolExecutor
Umsetzung hat sich geändert, so dass das Programm im original post sollte niemals zu verlassen.