我有一个扩展 Thread 类的类,如下所示。
public class RemoteDataSynchTaskProcessor extends Thread {
private GDRType taskGDR;
public RemoteDataSynchTaskProcessor(GDRType taskGDR) {
this.taskGDR = taskGDR;
}
private void execute() {
try {
try {
RemoteClient.runDataSynchTask(taskGDR);
} catch (RemoteException re) {
//.....
} catch (NotBoundException e) {
//......
}
processResult(false, "Success");
} catch (DTSException de) {
//....
}
}
public void run() {
Observer.incrementNumOfDataSyncyProcessor();
try {
execute();
} finally {
Observer.decrementNumOfDataSynchProcessor();
}
}
该线程执行完毕后,出现另一个线程,名为:Thread [pool-34-thread-1] (Running)
我不知道为什么这个线程出现并且永远不会终止,并且在几天后我最终得到了数千个线程,池数每次都在增加(例如线程 [pool-2500-thread-1])
如果调试并挂起这些生成的线程的执行,堆栈如下:
Thread [pool-1-thread-1] (Suspended)
Unsafe.park(boolean, long) line: not available [native method]
LockSupport.park(Object) line: 156
AbstractQueuedSynchronizer$ConditionObject.await() line: 1987
LinkedBlockingQueue<E>.take() line: 399
ThreadPoolExecutor.getTask() line: 947
ThreadPoolExecutor$Worker.run() line: 907
Thread.run() line: 662
这些线程是什么,我怎样才能摆脱它们?
编辑:似乎它与我正在使用的闭源线程池库有关(从询问执行程序服务的评论判断)。
我使用提供 ThreadPool 功能的库。这就是我在应用程序启动开始时创建池的方式。
ThreadPool fetcherPool = new ThreadPool("TASK_FETCHER", ConfigParams.minimumTaskFetcherCount, ConfigParams.maximumTaskFetcherCount, new TaskFetcher());
ThreadPool.java 好像是在扩展Java的ThreadGroup类,ThreadFetcher如下,其实是触发了线程。
public class TaskFetcher extends PooledMessageWorker //Pooled Message Worker extends ReacreatableThread
public void run() {
Observer.incrementNumOfTaskFetcher();
try {
while (true) {
try {
if (Observer.isShutdowned()) {
break;
}
ControllerUtil.getInstance().waitIfPaused();
ESDRType task = (ESDRType) TaskHandler.getTask();
if (task != null) {
trigger(task);
}
} catch (Exception e) {
LogUtil.error(logger, e.getMessage(), e);
}
}
} finally {
Observer.decrementNumOfTaskFetcher();
}
}
private void trigger(ESDRType task) {
try {
GDRType[] messages = createMessages(esdr);
for (GDRType message : messages) {
RemoteDataSynchTaskProcessor remoteCDCMTaskProcessor = new RemoteDataSynchTaskProcessor(message);
remoteCDCMTaskProcessor.start(); //This thread causes a new thread appear after execution finishes
LogUtil.debug(logger, "[distributeMessage()] [Message: ", message, "]... [OK]");
}
} catch (DTSException exception) {
// Logging stuff
}
}
}