日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java swing 多个线程,Swing与多线程

發布時間:2024/7/19 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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与多线程的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。