gpt4 book ai didi

java - 为什么等待线程会延迟 Swing 组件的更新?

转载 作者:行者123 更新时间:2023-12-01 17:56:28 26 4
gpt4 key购买 nike

我无法理解为什么在等待并行任务完成之前操作 swing 组件,即 JProgressBar

在下面的示例中,进度条只会在线程完成等待 Callable 的结果后激活其不确定模式,即使(我期望)调用 setIndependent( true) 发生在等待之前。有人可以解释为什么会发生这种情况吗?

private void thisDoesntWork(JProgressBar p){
ExecutorService executor = Executors.newSingleThreadExecutor();
Future<String> jobResult = executor.submit(() -> aLengthyJob());

// This happens only after the button finishes waiting. (future.get())
// I want to know why. Shouldn't this line happen before the thread blocks?
p.setIndeterminate(true);

try {
System.out.println(jobResult.get());
} catch (InterruptedException | ExecutionException ex) {}
}

public void createAndShowGUI(){
JFrame frame = new JFrame("This progress bar wont work");
JPanel panel = new JPanel();
JButton button = new JButton("Start");
JProgressBar progressBar = new JProgressBar();
frame.setSize(500, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

// Problem happens withing this button's action
button.addActionListener((e)->{
thisDoesntWork(progressBar);
});

panel.add(button);
panel.add(progressBar);
frame.add(panel);
frame.setVisible(true);

}

private String aLengthyJob(){
try {
Thread.sleep(10000);
} catch (InterruptedException ex) {}
return "Done";
}

public static void main(String[] args) {
new Test().createAndShowGUI();
}
<小时/>

如果等待发生在另一个线程中,它会按预期工作。

// Waiting in a third thread works fine
private void thisWorks(JButton b, JProgressBar p) {
p.setIndeterminate(true);
b.setEnabled(false);

ExecutorService executor = Executors.newFixedThreadPool(2);
Future<String> jobResult;

jobResult = executor.submit(() -> aLengthyJob());
executor.execute(() -> {
try {
System.out.println(jobResult.get());
p.setIndeterminate(false);
b.setEnabled(true);
} catch (InterruptedException | ExecutionException ex) {}
});
}
<小时/>

进口:

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;

最佳答案

jobResult.get() 等待 EDT 内另一个线程的结束,并将其阻塞。

Maybe you want to at least link to some background on edt.– GhostCat

以下是一些有关 EDT 的资源:

https://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html

What is the event dispatching thread?

关于java - 为什么等待线程会延迟 Swing 组件的更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44417059/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com