Proposal: Automatic Resource Management

Stefan Schulz schulz at e-spirit.de
Mon Mar 9 16:43:52 PDT 2009


Vilya Harvey wrote:
> I joined this mailing list too late to see the proposal when it was posted;
> does it include calling any kind of setup method on entering the block? If
> not, this may be worth considering as well?

The current proposal only has an exit method (close()). But I quite like 
the idea to generalize using resources with ARM instead of splitting it 
into two proposals. Wouldn't it be much more useful, if one introduces a 
completely new interface like the following:

interface ManagableResource {
   void acquire() throws Exception;
   void release() throws Exception;
}

Have a resource definition with an optional assignment:

try (<optional-assignment = > <resource-definition>) {
   // dostuff
}

That translates to (simple case):

$resource = <resource-definition>;
<optional-assignment = > $resource;
$resource.acquire();
try {
   // dostuff
} finally {
   $resource.release();
}

And retrofit Streams etc. (which are classes anyway) to implement 
ManagableResource (having an empty acquire() and a release() forwarding 
to close()) and add a ManagableLock that extends Lock and 
ManagableResource (does not support Lock directly, but code can be 
refactored to ManagableLock).

The try on a lock might be a bit misleading (like it is on a resource, 
as it does not try to get the resource), but I'd rather like to see one 
consistent solution than a half-hearty ARM plus some newly introduced 
construct re-using a modifier out of place.

Such a solution would require a bit more JDK-work though.

Stefan



More information about the coin-dev mailing list