Java:池的线程被阻塞而没有完成工作

时间:2018-03-22 17:57:12

标签: java multithreading threadpool java-threads

我有一个"被阻止的帖子"问题。让我解释这个应用程序是如何工作的这是一个文件处理器,它从目录中读取文件,对它们执行某些操作并将输出写入另一个目录。它具有从不同目录读取文件的线程,并将它们添加到队列中。每一个"阅读线程"有一个使用Executors.newFixedThreadPool(someNumber)线程构建的ExecutorService,它使用队列中的文件,处理它们并写入输出。

例如,现在以这种方式配置:

  • ReadingThread1 with 2 ProcessingThreads(pool-2-thread-x)
  • ReadingThread2 with 5 ProcessingThreads(pool-3-thread-x)
  • ReadingThread3 with 1 ProcessingThread(pool-4-thread-1)
  • ReadingThread4 with 1 ProcessingThread(pool-5-thread-1)

此应用程序已运行多年,现在我在文件处理中添加了另一种行为。问题是,经过几个小时(有时是6,8或20小时)运行后,一些线程被阻止,我们必须回滚到以前的版本。

这两个主题在同一时间或多或少被阻止。

Name: pool-4-thread-1
State: WAITING on java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject@64336985
Total blocked: 512  Total waited: 7,797

Stack trace: 
sun.misc.Unsafe.park(Native Method)
java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442)
java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1067)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1127)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
java.lang.Thread.run(Thread.java:745)

Last logged line:
13:32:49:830 [pool-4-thread-1] DEBUG ClassB:79 - MessageB

Name: pool-2-thread-2
State: WAITING on     java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject@65ffe5b2
Total blocked: 528  Total waited: 7,795

Stack trace: 
sun.misc.Unsafe.park(Native Method)
java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442)
java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1067)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1127)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
java.lang.Thread.run(Thread.java:745)

Last logged line:
13:35:18:997 [pool-2-thread-2] INFO  ClassA:110 - MessageA

这三个主题同时被或多或少地阻止了。

Name: pool-3-thread-1
State: WAITING on     java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject@2d9e6700
Total blocked: 1,506  Total waited: 7,421

Stack trace: 
sun.misc.Unsafe.park(Native Method)
java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442)
java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1067)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1127)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
java.lang.Thread.run(Thread.java:745)

Last logged line:
11:55:22:477 [pool-3-thread-1] INFO  ClassA:110 - MessageA


Name: pool-3-thread-4
State: WAITING on java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject@2d9e6700
Total blocked: 1,969  Total waited: 7,456

Stack trace: 
sun.misc.Unsafe.park(Native Method)
java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442)
java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1067)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1127)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
java.lang.Thread.run(Thread.java:745)

Last logged line:
11:55:03:962 [pool-3-thread-4] INFO  ClassA:110 - MessageA


Name: pool-3-thread-5
State: WAITING on java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject@2d9e6700
Total blocked: 1,747  Total waited: 7,306

Stack trace: 
sun.misc.Unsafe.park(Native Method)
java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442)
java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1067)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1127)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
java.lang.Thread.run(Thread.java:745)

Last logged line:
11:53:32:769 [pool-3-thread-5] INFO  ClassC:117 - MessageC

线程在sun.misc.Unsafe.park(Native Method)被阻止。我读到它只是等待新任务处理的线程。但在这种情况下,当应用程序开始运行线程池时,会创建并分配给在应用程序停止之前不会结束的任务。读取日志我可以看到线程在没有完成作业的情况下进入等待状态(MessageA,MessageB和MessageC不是最后记录的消息)。

我不明白这些线程是如何停止做他们正在做的事情(在不同的代码行中)并进入等待状态。

谢谢!

0 个答案:

没有答案