[英]Getting Amazing Exception with Threading In Java Swing
I am getting the following amazing exception when I am using multi-threading in my code but, I can not figure out where it occurs. 当我在代码中使用多线程时,出现了以下令人惊讶的异常,但我无法弄清楚它的发生位置。 Also this exception occurs only sometimes.
同样,此异常仅在某些时候发生。
Exception in thread "AWT-EventQueue-0" java.util.NoSuchElementException: Vector Enumeration at java.util.Vector$1.nextElement(Vector.java:348) at javax.swing.plaf.basic.BasicTableHeaderUI.getPreferredSize(BasicTableHeaderUI.java:793) at javax.swing.JComponent.getPreferredSize(JComponent.java:1660)
线程“ AWT-EventQueue-0”中的异常java.util.NoSuchElementException:java.util.Vector $ 1.nextElement(Vector.java:348)处的矢量枚举,javax.swing.plaf.basic.BasicTableHeaderUI.getPreferredSize(BasicTableHeaderUI.java :793),位于javax.swing.JComponent.getPreferredSize(JComponent.java:1660)
I am using the below code for multi-threading in my application. 我在我的应用程序中使用以下代码进行多线程处理。
try {
Thread Thread4 = new Thread() {
public void run() {
GetOrderData(mID, "OrderInfo_Orn");
}
};
Thread4.start();
Thread4.sleep(20);
Thread Thread5 = new Thread() {
public void run() {
GetOrderData(mID, "OrderInfo_Parts");
}
};
Thread5.start();
Thread5.sleep(20);
Thread queryThread = new Thread() {
public void run() {
GetMasterData(mID, rowId);
}
};
queryThread.start();
queryThread.sleep(20);
Thread Thread2 = new Thread() {
public void run() {
GetDetailData(mID, "'RcvPrePolishGoods_Detail'");
}
};
Thread2.start();
Thread2.sleep(20);
Thread Thread3 = new Thread() {
public void run() {
GetDetailData(mID, "'RcvPrePolishGoods_Parts'");
}
};
Thread3.start();
Thread3.sleep(20);
} catch (InterruptedException ex) {
Logger.getLogger(RcvPrePolishGoods.class.getName()).log(Level.SEVERE, null, ex);
}
Theres a couple things you want to look into: 您需要研究以下几件事:
If your updating the GUI (adding items to a JTable is what the exception implies) from the Get****Data
functions, you're going to run into EDT problems (that's the AWT-EventQueue-0
you see in the error). 如果您从
Get****Data
函数更新GUI(将异常添加到JTable中),您将遇到EDT问题(即您在错误中看到的AWT-EventQueue-0
) 。 Instead use SwingUtils.InvokeLater() . 而是使用SwingUtils.InvokeLater() 。 Or if you need it to run after 20 ms (or at 20 ms intervals), use a Swing Timer
或者,如果您需要它在20毫秒后(或以20毫秒为间隔)运行,请使用Swing计时器
You're calling threadname.start();
您正在调用
threadname.start();
and then in the next line, calling threadname.sleep(20);
然后在下一行中,调用
threadname.sleep(20);
. 。 According to the API ,
Thread.sleep
causes the currently running thread to wait - which in this case is the EDT (not threadname). 根据API ,
Thread.sleep
导致当前正在运行的线程等待-在这种情况下为EDT(不是线程名)。 If you want threadname to sleep, place the sleep inside the runnable like this: 如果您想让线程名进入睡眠状态,则将睡眠放置在可运行内部,如下所示:
Thread Thread5 = new Thread() { public void run() { Thread.sleep(); GetOrderData(mID, "OrderInfo_Parts"); } };
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.