在org.eclipse.jdt.ui.text.java.hover.IJavaEditorTextHover的getHoverInfo()中使用Display.getDefault()。syncexec()

时间:2013-12-03 16:56:14

标签: java eclipse-plugin hover thread-safety sync

我需要一个Thread来在IJavaEditorTextHover的getHoverInfo()方法中执行一些工作。但在我返回此方法的任何内容之前,我需要线程来完成它的工作。

我试过用while()来等待这样的线程:

但是方法getHoverInfo()仍然没有返回null或任何其他字符串。我需要在这个方法上返回一些特定的字符串,我需要线程来获取它。

我该怎么办?我如何等待Thread完成它的工作?

//新信息

我必须在getHoverInfo方法上启动一个Thread,我只需要在线程结束它的工作时执行其余的代码。 该线程只能在asyncexec上运行,因为如果它在syncexec上运行,getHoverInfo将永远不会返回任何内容 我不能在getHoverInfo中使用Thread.sleep或while()之类的东西。我试过,getHoverInfo()永远不会返回任何东西(我的意思是:即使方法返回null,javadoc也从未显示过,例如) 我的代码如下:

public String getHoverInfo(ITextViewer textViewer,IRegion hoverRegion){         if(hoverRegion!= null){

            /*comeca novo */
        ThreadGetHoverInfoExtensaoMostraExcecoes thread = new ThreadGetHoverInfoExtensaoMostraExcecoes();
            Display.getDefault().asyncExec(thread);

            /*termina novo */

            while(thread.endedJob == false)
            {
                //wait
            }
               return thread.getSomething();
    }
    return null;
}

1 个答案:

答案 0 :(得分:0)

如果您只是需要等待线程完成尝试Thread.join()

但是,自己管理线程通常是不明智的,请尝试使用执行程序并将您需要的工作提交为future task,您可以独立于该线程进行管理。

如果您在执行长时间运行任务时试图阻止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
            }
        } );
    }
}
相关问题