3

I am looking for a blocking version of AtomicReference to avoid such active waiting:

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 provides a Future interface, which blocks in get() method. But I cannot use that part of concurrent package because the reference should be set by a framework where the usage of a simple listener is expected.

To be more precise I work with the launching framework in Eclipse. I fire a maven launch via org.eclipse.m2e.actions.ExecutePomAction but I don't have a directly access to its process because it's hidden deeply in JDT. That's why I'm using Eclipse's launch manager for that purpose:

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);
    }
});

I think there is no other way to use active waiting afterwards, because IProcess provides no possibility to listen on its termination. Kinda like this:

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

This question has basically something common with [eclipse pde]How to catch the event that a launch is terminated? but it's quite old and I provided here more information on my investigations.

Community
  • 1
  • 1
Danny Lo
  • 1,553
  • 4
  • 26
  • 48
  • "should be set by a framework where the usage of a simple listener is expected" can you elaborate on that? What is your definition of a "simple listener"? – Fildor Nov 30 '15 at 12:14
  • Edited the question to add more details on my riddle. – Danny Lo Nov 30 '15 at 12:35

2 Answers2

2

You can use DebugPlugin.getDefault().addDebugEventFilter(filter) to set up an IDebugEventFilter class.

This is given all the debug / run events include the DebugEvent.TERMINATE event when a launch terminates.

greg-449
  • 109,219
  • 232
  • 102
  • 145
1

I also found another way to do it with a launch manager. I just had to use another listener interface:

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!
            }
        }
    }
});
Danny Lo
  • 1,553
  • 4
  • 26
  • 48