I am writing Eclipse plugins, and frequently have a situation where a running Job needs to pause for a short while, run something asynchronously on the UI thread, and resume.
So my code usually looks something like:
Display display = Display.getDefault(); display.syncExec(new Runnable() { public void run() { // Do some calculation // How do I return a value from here? } }); // I want to be able to use the calculation result here!
One way to do it is to have the entire Job class have some field. Another is to use a customized class (rather than anonymous for this and use its resulting data field, etc. What’s the best and most elegant approach?
You probably shouldn’t be assuming that the async
Runnablewill have finished by the time theasyncExeccall returns.In which case, you’re looking at pushing the result out into listeners/callbacks (possibly Command pattern), or if you do want to have the result available at a later in the same method, using something like a
java.util.concurrent.Future.