【发布时间】:2016-03-04 01:42:33
【问题描述】:
我正在寻找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? 有一些共同点,但它已经很老了,我在这里提供了有关我的调查的更多信息。
【问题讨论】:
-
“应该由一个预期使用简单监听器的框架设置”你能详细说明一下吗?您对“简单听众”的定义是什么?
-
编辑了问题以添加更多关于我的谜语的细节。
标签: java concurrency eclipse-plugin launching