我有一個swing應用程序。點擊菜單欄上的menuItem後,我試圖進行API調用,這將需要一些時間。在SwingUtilities.invokeLater()中殺死一個線程
我不希望Swing應用卡住/掛起,直到API調用給出結果,所以我使用了SwingUtilities.invokeLater來分離出一個新產生的線程來處理API調用。以下是上述
public void actionPerformed(ActionEvent e){
Object src=e.getSource();
if(src.equals(theJMenuItemClicked)){
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
//apicall here
}
})
}
代碼只要API調用線程運行我展示一個JDialog有消息說「進展API調用」和「中止線程」按鈕。
我想通過單擊Jdialog中的「ABort Thread」按鈕來終止線程。
如果是普通線程,我們有一個Thread t = new Thread(new Runnable())
,我們調用t.stop
。
如何獲取由SwingUtilities產生的特定線程實例,以便我可以在其上調用一個停止點?用更簡單的話來說,我怎樣才能殺死上面創建的線程?