根據JConsole堆棧跟蹤,我有一個生產者 - 消費者線程集,但生產者陷入不是.put()
的一行代碼中。生產者線程卡住但未阻塞
class Producer implements Runnable {
private final BlockingQueue<CopyOnWriteArrayList<Creature>> queue;
private World myWorld;
Producer(BlockingQueue<CopyOnWriteArrayList<Creature>> q, World myWorld) {
queue = q;
this.myWorld = myWorld;
}
public void run() {
int nextTick = myWorld.myApp.getTick(); //'tick' is the current frame our main loop is on.
while (true) {
if (myWorld.myApp.getTick() >= nextTick) { //if our world has updated to the next frame…
nextTick = myWorld.myApp.getTick() + 1; //increment the next frame to wait for
try {
for (int i = 0; i < myWorld.getCellController()
.getColumns(); i++) {
for (int j = 0; j < myWorld.getCellController()
.getRows(); j++) {
queue.put(myWorld.getCellController().Cells.get(i)
.get(j));
}
}
} catch (InterruptedException ex) {
System.out.println("INT! ******************************");
} catch (NullPointerException ex) {
System.out.println("NULL! ******************************");
} catch (ClassCastException ex) {
System.out.println("CAST! ******************************");
} catch (IllegalArgumentException ex) {
System.out.println("ARG! ******************************");
}
}
}
}
}
根據堆棧跟蹤,它只是停留在while(true)
線,而無需通過循環前進,即使它應該。
Stack trace:
Name: Producer
State: RUNNABLE
Total blocked: 0 Total waited: 196,958
Stack trace:
concurrency.Producer.run(Setup.java:25)
java.lang.Thread.run(Thread.java:680)
25行是while(true) {
行。
你正在使用哪個阻塞隊列的實現? – Perception 2013-02-17 05:02:27
我已經嘗試了'ArrayBlockingQueue'和'LinkedBlockingQueue',它與兩個掛起。 – honkbert 2013-02-17 05:06:04
請將(JConsole)堆棧跟蹤添加到您的問題中。 – Perception 2013-02-17 05:11:41