![](/img/trans.png)
[英]Display.getDefault().asyncExec gives InvocationTargetException
[英]using Display.getDefault().syncexec() inside getHoverInfo() of org.eclipse.jdt.ui.text.java.hover.IJavaEditorTextHover
我需要一個線程來執行IJavaEditorTextHover的getHoverInfo()方法內的某些工作。 但是在我對該方法返回任何內容之前,我需要線程來完成它的工作。
我試圖使用while()等待這樣的線程:
但是方法getHoverInfo()仍然不會返回null或任何其他字符串。 我需要在此方法上返回一些especific字符串,並且我需要線程來獲取它。
我能做什么? 我如何等待線程完成工作?
//新的信息
我必須在方法getHoverInfo上啟動一個線程,並且我需要其余的代碼僅在線程結束其工作時才能執行。 該線程只能在asyncexec上運行,因為如果它在syncexec上運行,則getHoverInfo將永遠不會返回任何我不能在Thread.sleep或getHoverInfo中使用while()之類的東西。 我試過了,getHoverInfo()永遠不會返回任何東西(我的意思是:即使該方法返回null,也永遠不會顯示javadoc)我的代碼如下:
公共字符串getHoverInfo(ITextViewer textViewer,IRegion hoverRegion){如果(hoverRegion!= null){
/*comeca novo */
ThreadGetHoverInfoExtensaoMostraExcecoes thread = new ThreadGetHoverInfoExtensaoMostraExcecoes();
Display.getDefault().asyncExec(thread);
/*termina novo */
while(thread.endedJob == false)
{
//wait
}
return thread.getSomething();
}
return null;
}
如果只需要等待線程完成,請嘗試Thread.join() 。
但是,通常不明智的做法是自己管理線程,嘗試使用執行程序並提交所需的工作作為將來可以獨立於線程進行管理的任務 。
如果要在執行某些長期運行的任務時阻止Swing阻塞,則需要將邏輯移出事件線程,執行工作,然后在完成后調回事件線程。 這是一個例子:
public static final Executor singleThreadedExecutor = Executors.newFixedThreadPool( 1 );
public void methodOnUIThread()
{
//Do normal callback stuff here
...
//Submit the threaded long running task, and return out of this method
singleThreadedExecutor.execute( new LongRunningWorkTask() );
}
private static class LongRunningWorkTask implements Runnable
{
@Override
public void run()
{
//todo do something long here
//Call back into the UI thread with an update
EventQueue.invokeLater( new Runnable()
{
@Override
public void run()
{
//todo: Update the UI thread here, now that the long running task is done
}
} );
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.