-
-
Save galderz/1029d0866d54930e88e7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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