新聞中心
為什么要加EventQueue.invokeLater呢,針對這個問題,這篇文章詳細介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
比如下面的程序:
import java.awt.*;
import javax.swing.*;
public class Test
{
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
JFrame frame = new JFrame();
frame.setSize(400, 300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
}
}
原因:
Java's GUI is strictly single-threaded.
All GUI related things in java should always go through a single thread. The thread is our legendary "AWT-EventQueue-0" . Hence all GUI related actions should necessarily go through the AWT Event thread. If not so you may end up in a deadlock. For small programs, this might never happen. But for a huge java application if you try frame.setVisible(true) kind of thing in main thread, you will soon find yourself searching a new job. What invokeLater() does is to post your Runnable in the AWT thread's event queue. So the code in your run method will be executed in the AWT-Eventqueue thread.
大意是說,java的GUI都是的單線程,應(yīng)該使用事件調(diào)度線程去執(zhí)行,如果沒意思使用事件調(diào)度線程的話,可能造成死鎖。但是在小的程序中,這種現(xiàn)象(死鎖)不會發(fā)生的;大的應(yīng)用程序中才會出現(xiàn)這種現(xiàn)象!
關(guān)于為什么要加EventQueue.invokeLater呢問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司行業(yè)資訊頻道了解更多相關(guān)知識。
網(wǎng)站標題:為什么要加EventQueue.invokeLater呢-創(chuàng)新互聯(lián)
網(wǎng)頁URL:http://fisionsoft.com.cn/article/cepdij.html