I have a method execute()
that runs a Runnable which is declared at the class level:
private Runnable r;
private Model m;
public Action(Model m) {
this.m = m;
r = () -> {
Operation op = new Operation(m);
op.executeAsync();
}
}
public void execute() {
//Do stuff here
r.run();
}
What is happening is the non-async tasks in execute()
are executed, and then r
is run to handle asynchronous work that takes several seconds to complete. However, the method does not return as soon as the Runnable is ran. Instead, it returns only when the async stuff is finished. However, if I also run the async tasks that are inside of op
in a thread, then it works as expected, with execute()
returning as soon as r
is ran, and the rest of the async work being done after the method ends. Why doesn't the code I am showing here work as I expected, without wrapping all the async work in op
in its own thread?