AtomicReference<T> 的阻塞版本 - 等待 Eclipse 启动终止

Blocking version of AtomicReference<T> - Wait for an Eclipse Launch to terminate

我正在寻找 AtomicReference 的阻塞版本以避免这种主动等待:

AtomicReference<Object> ref = new AtomicReference<Object>();
// execute some code in a different thread and set the reference
Object o;
while ((o = ref.get()) == null);
// continue execution

Java 提供了一个 Future 接口,它阻塞在 get() 方法中。但是我不能使用 concurrent 包的那部分,因为引用应该由一个框架设置,在这个框架中期望使用一个简单的监听器。

更准确地说,我使用 Eclipse 中的启动框架。我通过 org.eclipse.m2e.actions.ExecutePomAction 启动了 Maven,但我无法直接访问它的进程,因为它是 hidden deeply in JDT。这就是我为此目的使用 Eclipse 的启动管理器的原因:

final ILaunchManager launchMan = DebugPlugin.getDefault().getLaunchManager();
launchMan.addLaunchListener(new ILaunchListener() {
    public void launchRemoved(ILaunch launch) {
        ILaunchConfiguration conf = launch.getLaunchConfiguration();
        if (("Executing install in "+proj.getLocation().toOSString().replace('\', '-')).equals(conf.getName()))
        {
            IProcess[] processes = launch.getProcesses();
            if (processes.length == 1)
                procRef.set(processes[0]);
        }
        launchMan.removeLaunchListener(this);
    }
});

我认为之后没有其他方法可以使用主动等待,因为 IProcess 没有提供监听其终止的可能性。有点像这样:

BusyIndicator.showWhile(Display.getCurrent(), new Runnable() {
    public void run() {
        while (!proc.isTerminated())
            Thread.sleep(500);
    }
});

这个问题基本上与 [eclipse pde]How to catch the event that a launch is terminated? 有一些共同点,但它已经很老了,我在这里提供了更多关于我调查的信息。

您可以使用DebugPlugin.getDefault().addDebugEventFilter(filter)设置一个IDebugEventFilterclass。

这是给定的所有调试/运行 事件,包括启动终止时的 DebugEvent.TERMINATE 事件。

我还找到了另一种使用启动管理器的方法。我只需要使用另一个监听器接口:

final ILaunchManager launchMan = DebugPlugin.getDefault().getLaunchManager();
launchMan.addLaunchListener(new ILaunchesListener2()
{
    public void launchesTerminated(ILaunch[] launches)
    {
        for (ILaunch launch : launches) {
            ILaunchConfiguration conf = launch.getLaunchConfiguration();
            if (("Executing install in "+proj.getLocation().toOSString().replace('\', '-')).equals(conf.getName()))
            {
                //my maven launch is terminated!
            }
        }
    }
});