Given some arbitrary context (e.g. Junit unit test, not specifically just not necessarily the "main" thread).
Would code like this have to introduce at least 2 threads ?
public static void main (String[] args)
{
CompletableFuture<Void> s = new CompletableFuture<>();
CompletableFuture<Void> f = new CompletableFuture<>();
CompletableFuture<Void> someContext = CompletableFuture.supplyAsync(() ->
{
try{
System.out.println(Thread.currentThread().getId());
CompletableFuture<String> update =
CompletableFuture.supplyAsync(
() -> {
String ans = null;
try {
System.out.println(Thread.currentThread().getId());
ans = "Hello";
} catch (Exception e) {
ans = e.toString();
} finally {
s.complete(null);
return ans;
}
});
s.get();
System.out.println(s.isDone());
} catch (Exception e) {
System.out.println("Some error");
return null;
}
return null;
});
System.out.println(f.isDone());
}
When we get to s.get() in someContext can the JVM detect that it's waiting -> context switch to update complete it, and switch back to someContext?
When running it in ideone it consistently runs them in two diffrent threads but that's just single observation.
I would like to understand what guarantees the language/runtime provide.