基于Oracle Concurrency guidelines我正在尝试更新我的GUI。我的问题是,在EDT中的一个新线程中调用了GUI更新,这导致了我很多不同的失败。
以下代码已准备好运行,它是我的GUI更新示例,简单倒计时:
import java.awt.Component;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
public class Chronometer2 {
int COUNT = 5000;
CountTask countTask;
private static final Insets INSETS = new Insets(1, 1, 1, 1);
public Chronometer2(){
(countTask= new CountTask()).execute();
}
private static void addComponent(Container container, Component component, int gridx, int gridy,
int gridwidth, int gridheight, int anchor, int fill) {
GridBagConstraints gbc = new GridBagConstraints(gridx, gridy, gridwidth, gridheight, 1.0, 1.0,
anchor, fill, INSETS, 0, 0);
container.add(component, gbc);
}
public int returnCount(){
System.out.println("My count: " + COUNT);
return COUNT;
}
public void decreaseTime() {
COUNT--;
}
public int getTime() {
return COUNT;
}
private class CountTask extends SwingWorker<Void, Chronometer2>{
@Override
protected Void doInBackground() throws Exception {
// TODO Auto-generated method stub
final JFrame messageFrame = new JFrame();
final JLabel message = new JLabel("Next query in " + COUNT/1000 + " seconds.");
messageFrame.setLayout(new GridBagLayout());
messageFrame.setTitle("Warning");
messageFrame.setSize(500,100);
messageFrame.setLocationRelativeTo(null);
addComponent(messageFrame, message, 0, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.CENTER);
messageFrame.setVisible(true);
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
decreaseTime();
message.setText("Preventing connection block. Next query in " + COUNT/1000 + " seconds.");
if(getTime()==0){
messageFrame.dispose();
timer.cancel();
}
}
}, 1, 1);
return null;
}
}
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Chronometer2();
}
});
}
}
由于从主线程调用了样本,因此它正常工作。但是从我的应用程序调用是在EDT中进行的,因为我需要在显示消息之前按下按钮并触发操作。我创建了一个新线程来调用Swing实用程序(不能直接在EDT中调用),即使我遇到问题,因为直到现在我都无法生成EDT等待后台线程完成然后继续下一个查询。这是EDT中的一段代码:
Thread test = new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
try {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
new CountScreen();
}
});
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
test.start();
我还尝试用wait()
,notifiy()
和Timer Task处理这个问题,但问题是一样的;以下是准备运行的其他代码:
import java.awt.Component;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Chronometer {
private static final Insets INSETS = new Insets(1, 1, 1, 1);
private int TIMER = 3000;
public Chronometer(){
double timeToNext = System.currentTimeMillis() + 30000;
double timeDifference = 30000;
final JFrame messageFrame = new JFrame();
messageFrame.setLayout(new GridBagLayout());
messageFrame.setTitle("Warning");
final JLabel message = new JLabel("Next query in " + timeDifference/1000 + " seconds.");
messageFrame.setSize(500,100);
messageFrame.setLocationRelativeTo(null);
addComponent(messageFrame, message, 0, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.CENTER);
messageFrame.setVisible(true);
System.out.println(Thread.currentThread().getName());
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
synchronized (timer) {
decreaseTime();
message.setText("Preventing connection block. Next query in " + TIMER/1000 + " seconds.");
if(getTime()==0){
System.out.println("Im here");
timer.notify();
System.out.println("Im here 2");
messageFrame.dispose();
System.out.println("Im here 3");
timer.cancel();
}
}
}
}, 1, 1);
synchronized (timer) {
try {
timer.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("Done");
}
private static void addComponent(Container container, Component component, int gridx, int gridy,
int gridwidth, int gridheight, int anchor, int fill) {
GridBagConstraints gbc = new GridBagConstraints(gridx, gridy, gridwidth, gridheight, 1.0, 1.0,
anchor, fill, INSETS, 0, 0);
container.add(component, gbc);
}
public void decreaseTime() {
TIMER--;
}
public int getTime() {
return TIMER;
}
public static void main(String[] args){
Chronometer c = new Chronometer();
}
}
所以,我差不多3天想知道,我怎么能解决这个问题呢?任何建议都非常受欢迎;提前谢谢。
答案 0 :(得分:1)
我找到了自己的答案。停止EDT绝不是一个选择。决不。如果你想像我一样处理一个问题,解决方案比我想象的要简单得多。触发后,侦听器中的操作只是调用另一个线程来处理您想要的任何内容。这可以防止EDT变忙并冻结GUI。这样,您将能够尽可能多地锁定线程,并且我的意思是同步方法,循环或您想要使用的其他内容。我的解决方案我可以考虑阅读这篇文章:main and the GUI Event Dispatch。这真是一篇很棒的文章,让我开始关注摇摆和线程的新内容。希望这对你也有帮助。