我對Java中的多線程有點困惑,我有一個GUI,我創建了一個線程,另一個項目充當服務器並用於從其他數據源接收數據是在一個單獨的線程。服務器的線程在GUI線程的一個視圖中調用方法並更新狀態,但GUI不更新。我怎樣才能正確設置這個架構。下面是我有:從另一個線程更新GUI線程
public static void main(String[] args)
{
//Connections
Runnable r2 = new Runnable() {
@Override
public void run()
{
App.connectToServer();
}
};
//Launch main window
Runnable r1 = new Runnable() {
@Override
public void run()
{
//Installs theme
WebLookAndFeel.install();
//Launches main window
BootWindow myMainWindow = new BootWindow();
}
};
Thread thr1 = new Thread(r1);
Thread thr2 = new Thread(r2);
thr1.start();
thr2.start();
}
//Server connections
private static void connectToServer()
{
System.out.println("Connecting to the eTrade Manager Server..");
etmServer server = new etmServer();
server.connectEtmServer();
}
閱讀Swing中的Oracle trail Concurency, EventDispatchThread – mKorbel 2014-10-08 15:07:24
[請閱讀偶數調度線程](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html),因爲您需要更新其上的任何GUI組件。 – 2014-10-08 15:07:37
回答[這裏](http://stackoverflow.com/a/26267512/230513)。 – trashgod 2014-10-08 22:35:11