3

I am executing a certain flow wherein I submit some task to Callable and store the output in Future<> future. In some of the cases I am not calling future.get() to retrieve the value nor am I cancelling the task. Can this cause any issue?

xingbin
  • 27,410
  • 9
  • 53
  • 103
Faiz Kidwai
  • 463
  • 5
  • 26

2 Answers2

3

In most cases, when you do not care the execution result of this task, it will not cause any issue.

But please notice this special function of future.get:

Memory consistency effects: Actions taken by the asynchronous computation happen-before actions following the corresponding Future.get() in another thread.

Here is an example:

int i = 0;

and if you let

i = 1;

in the task, then read i after future.get, it can gurantees you get the fresh value 1.

Without calling future.get, you might get the obsolete value 0.

xingbin
  • 27,410
  • 9
  • 53
  • 103
2

I am executing a certain flow wherein I submit some task to Callable and store the output in Future<> future. In some of the cases I am not calling future.get() to retrieve the value nor am I cancelling the task. Can this cause any issue?

No it won't cause any issues. The future will be garbage collected as long as no one is holding a reference to it.

Obviously you could submit a Runnable instead of the Callable if you don't need the results from the call() method but it sounds to me like you want to programatically chose to just forget about the Callable which is fine.

Gray
  • 115,027
  • 24
  • 293
  • 354