Java SwingWorker 挂起
我正在调试一些使用 SwingWorker
编写的代码,以执行数值计算和 GUI 更新的组合。 SwingWorker
挂起并显示以下堆栈跟踪:
Full thread dump Java HotSpot(TM) Client VM (14.3-b01 mixed mode, sharing):
"SwingWorker-pool-3-thread-4" prio=6 tid=0x07fd7c00 nid=0x143c waiting on condition [0x0a33f000]
java.lang.Thread.State: TIMED_WAITING (sleeping)
at java.lang.Thread.sleep(Native Method)
at sum.ee.ui.modelviewer.ModelViewer$ModelAnimator.doInBackground(ModelViewer.java:940)
at sum.ee.ui.modelviewer.ModelViewer$ModelAnimator.doInBackground(ModelViewer.java:877)
at javax.swing.SwingWorker$1.call(SwingWorker.java:274)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at javax.swing.SwingWorker.run(SwingWorker.java:313)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)
"SwingWorker-pool-3-thread-3" prio=6 tid=0x07fd7000 nid=0x11a8 waiting for monitor entry [0x0a2af000]
java.lang.Thread.State: BLOCKED (on object monitor)
at java.awt.Component.resize(Component.java:2044)
- waiting to lock <0x24b936a0> (a java.awt.Component$AWTTreeLock)
at java.awt.Component.setSize(Component.java:2035)
at java.awt.Component.resize(Component.java:2069)
at java.awt.Component.setSize(Component.java:2060)
at javax.swing.JViewport.setViewSize(JViewport.java:1038)
at javax.swing.ViewportLayout.layoutContainer(ViewportLayout.java:183)
at java.awt.Container.layout(Container.java:1421)
at java.awt.Container.doLayout(Container.java:1410)
at jsyntaxpane.components.LineNumbersRuler.updateSize(LineNumbersRuler.java:109)
at jsyntaxpane.components.LineNumbersRuler.removeUpdate(LineNumbersRuler.java:203)
at javax.swing.text.AbstractDocument.fireRemoveUpdate(AbstractDocument.java:243)
at jsyntaxpane.SyntaxDocument.fireRemoveUpdate(SyntaxDocument.java:118)
at javax.swing.text.AbstractDocument.handleRemove(AbstractDocument.java:608)
at javax.swing.text.AbstractDocument.remove(AbstractDocument.java:576)
at javax.swing.JEditorPane.setText(JEditorPane.java:1493)
at sum.ee.ui.SourceCodePanel.clearSourcePane(SourceCodePanel.java:256)
at sum.ee.ui.SourceCodePanel.access$100(SourceCodePanel.java:47)
at sum.ee.ui.SourceCodePanel$1.stateChanged(SourceCodePanel.java:209)
at sum.ee.ui.VisualizationAggregator.fireStateChanged(VisualizationAggregator.java:300)
at sum.ee.ui.VisualizationAggregator.update(VisualizationAggregator.java:97)
at sum.ee.ui.modelviewer.ModelViewer$ModelAnimator.doInBackground(ModelViewer.java:918)
at sum.ee.ui.modelviewer.ModelViewer$ModelAnimator.doInBackground(ModelViewer.java:877)
at javax.swing.SwingWorker$1.call(SwingWorker.java:274)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at javax.swing.SwingWorker.run(SwingWorker.java:313)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor
$Worker.run(ThreadPoolExecutor.java:908) at java.lang.Thread.run(Thread.java:619)
我的理解是 GUI 工作不应该在 doInBackground()
内部完成,而应该在 done()
中完成>。我进行了一个幼稚的实验,将 doInBackground()
中的所有代码移至 done()
中,但它仍然不起作用。人们可以给我一些建议,告诉我如何从根本上解决这个问题吗?代码如下所示:
protected Void doInBackground() {
isAnimating = true;
resetButtonBackgrounds();
backgroundColor = new Color(175, 255, 175); // Soft Green
JToggleButton b = null;
for (final int index : modelIndices) {
if (index == modelIndices.get(modelIndices.size() - 1)) {
backgroundColor = defaultBackgroundColor;
}
if (!keepTrace) {
// Resetting the backgrounds is necessary to have
// individual display of the changing elements due to
// the fact that there can be multiple nodes per
// source line. The reset works in combination
// with updating from ModelViewer.this (as opposed
// to the 'this' of ModelAnimator instances) due
// to not sending an event to itself. Furthermore,
// if the event was sent from ModelAnimator, the model
// indices are recalculated, causing a jump when multiple
// element source lines are encountered.
resetButtonBackgrounds();
}
aggregator.modelIndex(index);
aggregator.update(ModelViewer.this);
b = getButtonByIndex(index);
scrollRectToVisible(b.getBounds());
ModelViewer.this.repaint();
try {
StaticTools.sleepAtLeast(sleepTimeMilliseconds);
} catch (final InterruptedException ex) {
// continue with thread
}
}
isAnimating = false;
if ( b != null) {
Color orig = b.getBackground();
Color blink = Color.PINK;
Color current = orig;
for (int i = 0; i < 100; i++) {
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
}
if (current == orig) {
current = blink;
} else {
current = orig;
}
b.setBackground(current);
ModelViewer.this.repaint();
}
}
return null;
}
另一个线索是有两个 SwingWorker
线程在执行。他们可以运行同一个线程吗?
更新:以下是执行SwingWorker
的代码:
public final void animate(final long delayBetweenUpdatesMilliseconds,
final List<Integer> modelIndices,
final boolean keepTrace,
final List<PropertyChangeListener> propertyChangeListeners) {
ModelAnimator modelAnimator =
new ModelAnimator(
delayBetweenUpdatesMilliseconds,
modelIndices,
keepTrace);
for (final PropertyChangeListener listener : propertyChangeListeners) {
modelAnimator.addPropertyChangeListener(listener);
}
modelAnimator.execute();
}
I'm debugging some code that was written using a SwingWorker
to perform a mix of numerical calculation and GUI update. The SwingWorker
hangs with the following stack trace :
Full thread dump Java HotSpot(TM) Client VM (14.3-b01 mixed mode, sharing):
"SwingWorker-pool-3-thread-4" prio=6 tid=0x07fd7c00 nid=0x143c waiting on condition [0x0a33f000]
java.lang.Thread.State: TIMED_WAITING (sleeping)
at java.lang.Thread.sleep(Native Method)
at sum.ee.ui.modelviewer.ModelViewer$ModelAnimator.doInBackground(ModelViewer.java:940)
at sum.ee.ui.modelviewer.ModelViewer$ModelAnimator.doInBackground(ModelViewer.java:877)
at javax.swing.SwingWorker$1.call(SwingWorker.java:274)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at javax.swing.SwingWorker.run(SwingWorker.java:313)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)
"SwingWorker-pool-3-thread-3" prio=6 tid=0x07fd7000 nid=0x11a8 waiting for monitor entry [0x0a2af000]
java.lang.Thread.State: BLOCKED (on object monitor)
at java.awt.Component.resize(Component.java:2044)
- waiting to lock <0x24b936a0> (a java.awt.Component$AWTTreeLock)
at java.awt.Component.setSize(Component.java:2035)
at java.awt.Component.resize(Component.java:2069)
at java.awt.Component.setSize(Component.java:2060)
at javax.swing.JViewport.setViewSize(JViewport.java:1038)
at javax.swing.ViewportLayout.layoutContainer(ViewportLayout.java:183)
at java.awt.Container.layout(Container.java:1421)
at java.awt.Container.doLayout(Container.java:1410)
at jsyntaxpane.components.LineNumbersRuler.updateSize(LineNumbersRuler.java:109)
at jsyntaxpane.components.LineNumbersRuler.removeUpdate(LineNumbersRuler.java:203)
at javax.swing.text.AbstractDocument.fireRemoveUpdate(AbstractDocument.java:243)
at jsyntaxpane.SyntaxDocument.fireRemoveUpdate(SyntaxDocument.java:118)
at javax.swing.text.AbstractDocument.handleRemove(AbstractDocument.java:608)
at javax.swing.text.AbstractDocument.remove(AbstractDocument.java:576)
at javax.swing.JEditorPane.setText(JEditorPane.java:1493)
at sum.ee.ui.SourceCodePanel.clearSourcePane(SourceCodePanel.java:256)
at sum.ee.ui.SourceCodePanel.access$100(SourceCodePanel.java:47)
at sum.ee.ui.SourceCodePanel$1.stateChanged(SourceCodePanel.java:209)
at sum.ee.ui.VisualizationAggregator.fireStateChanged(VisualizationAggregator.java:300)
at sum.ee.ui.VisualizationAggregator.update(VisualizationAggregator.java:97)
at sum.ee.ui.modelviewer.ModelViewer$ModelAnimator.doInBackground(ModelViewer.java:918)
at sum.ee.ui.modelviewer.ModelViewer$ModelAnimator.doInBackground(ModelViewer.java:877)
at javax.swing.SwingWorker$1.call(SwingWorker.java:274)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at javax.swing.SwingWorker.run(SwingWorker.java:313)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor
$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)
My understanding is that GUI work should not be done inside of doInBackground()
, but rather in done()
. I performed a naive experiment where I moved all of the code in doInBackground()
into done()
and it still didn't work. Is there any tips that folks can give me on what I can do to root cause this issue? The code looks like this:
protected Void doInBackground() {
isAnimating = true;
resetButtonBackgrounds();
backgroundColor = new Color(175, 255, 175); // Soft Green
JToggleButton b = null;
for (final int index : modelIndices) {
if (index == modelIndices.get(modelIndices.size() - 1)) {
backgroundColor = defaultBackgroundColor;
}
if (!keepTrace) {
// Resetting the backgrounds is necessary to have
// individual display of the changing elements due to
// the fact that there can be multiple nodes per
// source line. The reset works in combination
// with updating from ModelViewer.this (as opposed
// to the 'this' of ModelAnimator instances) due
// to not sending an event to itself. Furthermore,
// if the event was sent from ModelAnimator, the model
// indices are recalculated, causing a jump when multiple
// element source lines are encountered.
resetButtonBackgrounds();
}
aggregator.modelIndex(index);
aggregator.update(ModelViewer.this);
b = getButtonByIndex(index);
scrollRectToVisible(b.getBounds());
ModelViewer.this.repaint();
try {
StaticTools.sleepAtLeast(sleepTimeMilliseconds);
} catch (final InterruptedException ex) {
// continue with thread
}
}
isAnimating = false;
if ( b != null) {
Color orig = b.getBackground();
Color blink = Color.PINK;
Color current = orig;
for (int i = 0; i < 100; i++) {
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
}
if (current == orig) {
current = blink;
} else {
current = orig;
}
b.setBackground(current);
ModelViewer.this.repaint();
}
}
return null;
}
The other clue is that there are two SwingWorker
threads that execute. Can they be running the same thread?
UPDATE: Here is the code that executes the SwingWorker
:
public final void animate(final long delayBetweenUpdatesMilliseconds,
final List<Integer> modelIndices,
final boolean keepTrace,
final List<PropertyChangeListener> propertyChangeListeners) {
ModelAnimator modelAnimator =
new ModelAnimator(
delayBetweenUpdatesMilliseconds,
modelIndices,
keepTrace);
for (final PropertyChangeListener listener : propertyChangeListeners) {
modelAnimator.addPropertyChangeListener(listener);
}
modelAnimator.execute();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是对 Swing EDT 规则的不遵守。
SwingWorker 的目的是在发生 UI 事件时执行繁重的非 GUI 任务,否则会阻塞 UI,然后在最后更新 UI。
所以你可以在 doInBackground(); 中实现举重。然后,一旦完成 swing,就会在 EDT 上调用 done(),并且您可以使用 get() 检索结果。
这里的问题是您正在 SwingWorker 创建的新线程中执行 GUI 工作。这可能会导致死锁和并发问题。
这包括创建所述 GUI 对象,即使您已经在 EDT
操作中,这些对象也应该位于可运行对象中,例如:
应该使用 InvokeandWait 封装在可运行对象中。实际修改 GUI 本身的东西尤其需要在它们自己的可运行对象中,即使如果您已经在响应按钮按下或更改的 swing 事件调度线程中,您也会冒着处理您的对象的风险。已经在工作了。
例如,swing 正在工作并锁定 A 以让您工作,让您在 B 上工作尝试锁定并在 A 上工作
This is a failure to observe the Swing EDT rules.
The purpose of SwingWorker is to do heavy nonGUI tasks when a UI event occurs that would otherwise block the UI, then update the UI at the end.
So you would implement your weight lifting inside doInBackground(); then once finished swing would call done() on the EDT and you can retrieve the results using get().
The problem here is you are doing GUI work in the new thread SwingWorker has created. This can lead to deadlocks and concurrency issues.
This includes the creation of said GUI objects, which should be in runnables even if your already on the EDT
Actions such as:
Should be encased in a Runnable with InvokeandWait. Stuff that actually modifies the GUI itself especially need to be in their own runnables, even if your already in the swing event dispatch thread responding to a button press or change, you run the risk of working on objects your already working on.
e.g. swing is working on and locked A to let you do work, letting you do work on B attempting to lock and work on A