Search code examples
javafuturetask

Cleanup FutureTask after using


I wonder if after the FutureTask finishes his job and I can get the result from it successfully, should I cleanup the FutureTask to release the resource ? Or the FutureTask will be cleaned up automatically after a certain amount of time after it finished the job ?

Thank you.


Solution


  • The FutureTask is kind of immutable. Once its value, and state have been set they cannot be changed. Apart from this, the FutureTask is like any other Java object, so you don't have to do anything. It is eligible for GC as soon as there are no more references to it. This means, it will be GCed once it goes out of scope, or if you insist you can remove the reference by assigning null to it. If you have written your code well, you shouldn't set null to any reference - every object should live in the smallest scope possible.