2

Let's say I do something in Java like:


RemoteResponse response = null;
try {
   FutureTask task new FutureTask(....);
   executor.execute(task);
   response = task.get(1000, TimeUnits.MILLISECONDS);
}
catch( TimeoutException te ) {
   
   .. should I do something special here? ...
   .. what happens to the return value of the task if task.get() throws an exception? ...
   .. is it ever garbage collected? ..
   
}

My question is does something hold onto RemoteResponse in the case where TimeoutException is thrown? Will it get garbage collected? Do I have to call the cancel() method on the task for that to happen?

4

2 回答 2

3

Edit after the question was revised:

response is a reference to a RemoteResponse that task is responsible for allocating. The assignment of the return value from a method won't happen if the method threw an exception, so there is no need for special handling of response.

task will be unreferenced when it goes out of scope, either through normal execution or if an exception is thrown.

If the resources allocated by task are well encapsulated, ie there are no externally held references, and are released (close, release, whatever), then there should be no resource leakage.

There's no need to call cancel unless there is some shared resource the task has exclusively or some other consumable resource that the rest of the application needs.

I'd at least log the fact that the task didn't complete in the time allotted. What else you do depends on the requirements of your application.

Note that task will continue executing until completion, regardless of the get call.

于 2008-11-10T21:56:49.533 回答
1

I think the way to look at the problem is that you need to claim the resource outside of the FutureTask, so that when you do decide to cancel it you can force the reclamation of resources.

so:

Resource res = null;
try {
 resource = ResourceAquirer.claim()

 FutureTask<?> task = new FutureTask<?>(resource);
 executor.execute(task);   
 response = task.get(1000, TimeUnits.MILLISECONDS);
} catch (Exception e) {
  // logging
} finally {
 if (resource != null) {
   resource.release();
 }
}

This way you can be sure that the resource will be freed. It is really frustrating that all of that cannot be encapuslated inside the future task, but I cannot find proof that calling cancel on a futuretask will ensure that a finally block is invoked inside the FutureTask. (Maybe I'll ask that as another question)

于 2009-09-16T14:10:51.080 回答