if i have one (or more) CompletableFuture
not started yet, and on that method(s) a few thenApplyAsync()
, anyOf()
-methods.
Will the Garbage Collector remove all of that?
If there is a join()
/get()
at the end of that chain -> same question: Will the Garbage Collector remove all of that?
Maybe we need more information about that context of the join().
That join is in a Thread the last command, and there are no side-effects. So is in that case the Thread still active? - Java Thread Garbage collected or not
Anyway is that a good idea, to push a poisen-pill down the chain, if im sure (maybe in a try-catch-finally), that i will not start that Completable-chain, or is that not necessary?
The question is because of something like that? (https://bugs.openjdk.java.net/browse/JDK-8160402)
Some related question to it: When is the Thread-Executor signaled to shedule a new task? I think, when the CompletableFuture
goes to the next chained CompletableFuture
?. So i must only carry on memory-leaks and not thread-leaks?
Edit: What i mean with a not started CompletableFuture?
i mean a var notStartedCompletableFuture = new CompletableFuture<Object>();
instead of a CompletableFuture.supplyAsync(....);
I can start the notStartedCompletableFuture in that way:
notStartedCompletableFuture.complete(new Object);
later in the program-flow or from another thread.
Edit 2: A more detailed Example:
AtomicReference<CompletableFuture<Object>> outsideReference=new AtomicReference<>();
final var myOuterThread = new Thread(() ->
{
final var A = new CompletableFuture<Object>();
final var B = new CompletableFuture<Object>();
final var C = A.thenApplyAsync((element) -> new Object());
final var D = CompletableFuture.anyOf(A, C);
A.complete(new Object());
// throw new RuntimeException();
//outsideReference.set(B);
----->B.complete(new Object());<------ Edit: this shouldn't be here, i remove it in my next iteration
D.join();
});
myOuterThread.start();
//myOutherThread variable is nowhere else referenced, it's sayed so a local variable, to point on my text on it^^
- So in the normal case here in my example i don't have a outside
reference. TheCompletableFutures
in the thread have never a chance getting completed. Normally the GC can safely erase both the thread and and the content in there, theCompetableFutures
. But i don't think so, that this would happen? - If I abbord this by throwing an exception -> the
join()
is never reached, then i think all would be erased by the GC? - If I give one of the
CompletableFutures
to the outside by thatAtomicReference
, there then could be an chance to unblock thejoin()
, There should be no GC here, until the unblock happens. BUT! the waitingmyOuterThread
on thatjoin()
doesen't have to to there anything more after thejoin()
. So it could be an optimization erasing that Thread, before someone from outsidecompletes
B
. But I think this would be also not happen?!
One more question here, how I can proof that behavior, if threads are blocked by waiting on a join()
or are returned to a Thread-Pool?, where the Thread also "blocks"?