为什么等待线程会延迟对 swing 组件的更新?
Why is waiting for a thread delaying updates to swing components?
我无法理解为什么要在等待并行任务完成之前操纵摆动组件,即 JProgressBar
。
在下面的示例中,progressBar 将仅在线程完成等待 Callable
的结果后激活其不确定模式,即使(我预计)发生了对 setIndeterminate(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?
我无法理解为什么要在等待并行任务完成之前操纵摆动组件,即 JProgressBar
。
在下面的示例中,progressBar 将仅在线程完成等待 Callable
的结果后激活其不确定模式,即使(我预计)发生了对 setIndeterminate(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?