java swing 多个线程,Swing与多线程
1)如果要在圖形界面上顯示經過數據查詢或經過其他方式得來的數據,一般將這個過程放在一個線程中,由該線程進行單獨運算,并隨時更新圖形界面。
(2)Swing線程發生死鎖的時候,如果是使用命令行的方式運行的該程序,可以使用CTRL+BREAK的快捷鍵,會得到線程死鎖的位置,堆棧的一些信息。
(3)只有與Swing相同的線程才能對Swing中的組件進行調用,修改等,如ActionListener中的處理就是與Swing保持在同一個線程侯中,不在同一個線程時,需使用SwingUtilities.invokeLater()。但需注意的是invokeLater在工作時都會使Swing主線程的窗口繪制工作停下了,直到invokeLater結束,所以在invokeLater中不要做一些耗時的工作,盡量只做與界面更新相關的工作。 SwingUtilities.isEventDispatchThread():return true if the current thread is an awt event dispatching thread.該方法可以判斷當前線程是否awt事件線程,如果不是對于界面的更新操作就應放在invokeLater中。
(4)Swing是一個基于事件隊列的單線程模型,GUI上的事件,一個個依次awt event dispatching thread執行,不會發生搶奪資源的情況。這個事件隊列就是java.awt.EventQueue. EventQueue is a platform-dependent class that queues event, both?from the underlying peer classes and from the trusted application classes. It encapsulates asychronous event dispatch machinary which extracts events from the queue and dispatch them by calling dispatchEvent(AWTEvent) method on this EventQueue with the event to be dispatched? as an argument. The requriements for the events are:Sequentially, that is, it s not permited that serveral events are dispatched simoultanously and the order to be dispatched?should be as they are?enqueued. And the awt event dispatching thread is fired by the envent dispatch machinary. InvokeLater(Runnable doRun) method causes the doRun.run() to be executed asynchronously on AWT Event dispatch thread, and this will happen after all pending AWT events have been processed. This method should be used when an application thread needs to update the GUI.InvokeAndWait(Runnable doRun) method causes the doRun.run() to be executed sychronously on the AWT event dispatch thread, and this call block uitil all pending AWT events have been processed and then doRun.run() returns.
(5)比如要在一個按鈕上又一個操作耗費時間較多,應將此業務代碼放在一個線程中執行,執行完畢后再在Dispatch Thread執行Swing組件更新代碼。下文中使用SwingWorker在1.6中已經存在,1.5的可以到https://swingworker.dev.java.net/下載。注意這里不能使用invokeLater,應創建一個線程類或使用SwingWorker
publicActionListener?getSaveAsActionListener()?{
if(this.saveAsActionListener?==null)?{
this.saveAsActionListener?=newActionListener()?{
/**
*響應點擊另存為按鈕的事件的方法
*/
publicvoid?actionPerformed(ActionEvent?e)?{
finalSwingWorker?worker?=newSwingWorker()?{
@Override
publicObject?construct()?{
try{
getJEditorPane1().fireControllerChangeListener();
returnDiagramDesignerJFrame.serviceFinished;
}?catch(DocumentException?e1)?{
e1.printStackTrace();
JOptionPane.showMessageDialog(
DiagramDesignerJFrame.this,"您的輸入不符合xml格式要求!"
+?e1.getMessage());
}?catch(Exception?e1)?{
e1.printStackTrace();
}
returnnull;
}
/**
*執行完構造器后,在GUI上異步執行它。
*/
publicvoid?finished()?{
saveAction();
}
};
worker.start();
}
};
}
returnsaveAsActionListener;
}
(6)在進行Swing開發時遇到一個異常:?Exception in thread "Thread-5" java.lang.ClassCastException: sun.java2d.NullSurfaceData cannot be cast to??? sun.java2d.d3d.D3DSurfaceData
at sun.java2d.d3d.D3DRenderer.copyArea(Unknown Source)
at sun.java2d.d3d.D3DSurfaceData.copyArea(Unknown Source)
at sun.java2d.SunGraphics2D.doCopyArea(Unknown Source)
at sun.java2d.SunGraphics2D.copyArea(Unknown Source)
at javax.swing.BufferStrategyPaintManager.copyArea(Unknown Source) 到網絡上搜索也沒有找到確切答案,但是根據以上原則,發現自己確實違反了一點,在一個線程中直接調用table的更新操作,之后將該table的更新操作放到invokeLater中,后來就沒有了
總結
以上是生活随笔為你收集整理的java swing 多个线程,Swing与多线程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: iis设置导致python调用urlli
- 下一篇: add结果 bigdecimal_jav