Skip to content

Instantly share code, notes, and snippets.

@galderz
Last active October 14, 2015 10:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save galderz/1029d0866d54930e88e7 to your computer and use it in GitHub Desktop.
Save galderz/1029d0866d54930e88e7 to your computer and use it in GitHub Desktop.
Transaction suspendedTx = transactionManager.suspend(); // executed in thread T1
R return = CompletableFuture.supplyAsync(() -> {
transactionManager.resume(suspendedTx); // executed in thread T2
R ret = ... // compute R
return R;
}
// Q: How do you get thread T1 to resume the suspended transaction after CompletableFuture completes?
Transaction suspendedTx = transactionManager.suspend(); // executed in thread T1
R return = CompletableFuture.supplyAsync(() -> {
transactionManager.resume(suspendedTx); // executed in thread T2
R ret = ... // compute R
return R;
}.whenComplete((ret, t) -> {
// This is executed with T2 so it does not work, it needs to be executed with T1
transactionManager.resume(suspendedTx);
});
WithThreadExecutor resumeExecutor = new WithThreadExecutor(Thread.currentThread()); // current thread is T1
R return = CompletableFuture.supplyAsync(() -> {
transactionManager.resume(suspendedTx); // executed in thread T2
R ret = ... // compute R
return R;
}.whenCompleteAsync((ret, t) -> {
transactionManager.resume(suspendedTx); // Does not get executed :(
}, resumeExecutor);
private static class WithThreadExecutor implements Executor {
final Thread t;
private WithThreadExecutor(Thread t) { this.t = t; }
public void execute(Runnable command) {
// execute() callback executed but command itself not executed
Executors.newSingleThreadExecutor(r -> t).submit(command).get(5, TimeUnit.SECONDS);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment