oxuser
oxuser

Reputation: 1327

If you discard a reference to a Future, will it still run?

In my method, I have to call another method (AnotherMethod) that returns a future.

eg.

private static void myMethod() {
    Future<MyObj> mjObj = AnotherMethod();
    return;
}

I don't actually care about the value returned by AnotherMethod (eg. the value of myObj), but I do want AnotherMethod to run fully.

If I discard the reference to the future (as in the above example), will AnotherMethod still finish running?

I understand it won't finish before returning from myMethod, but will it still complete at some point even though there's no reference to myObj anymore?

Upvotes: 3

Views: 166

Answers (1)

jreznot
jreznot

Reputation: 2773

First of all, AnotherMethod always will be performed from the start to the end because you call it. As for concurrency, if AnotherMethod starts a Thread or submits task to an executor then this concurrent execution will not be interrupted. Garbage collector does not interrupt threads because they are GC roots - top level objects in JVM.

Upvotes: 2

Related Questions