river-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Peter Firmstone <j...@zeus.net.au>
Subject Re: A non blocking (almost) DynamicPermissionCollection
Date Mon, 19 Dec 2011 19:14:04 GMT
Gregg Wonderly wrote:
> On 12/18/2011 11:59 PM, Peter Firmstone wrote:
>>    @Override
>>    public boolean implies(Permission permission) {
>>        if ( ! cl.isInstance(permission)) return false;
>>        Permission [] p = perms.toArray(new Permission[0]); 
>> //perms.size() may change
>
> I not sure why you are using an empty Permission array here, and then 
> looping over it.  Is this from some other testing?
>
> Gregg
>
The collection is wrapped with a ConcurrentCollection's multi read, 
single write wrapper, by passing a zero length array to the collection, 
it obtains a read lock, the collection creates a new array, sized to 
suit the underlying array, copy's the collection's contents into it and 
returns, releasing the read lock, the operation is atomic.  

If I did this, the call would acquire and release two read locks 
consecutively, it wouldn't be atomic:

Permission[] p  = perms.toArray(new Permission[perms.size()]);

It's there to avoid a cast:

Permission [] p = (Permission[]) perms.toArray();

Although not utilised here the Iterator returned by the 
ConcurrentCollection wrapper has a private copy of the underlying 
Collection, so it will never throw ConcurrentModificationException, so I 
could use that instead, but using the array consumes less memory than 
duplicating the collection.

Maybe I should change the comment to // Zero length array is replaced by 
correct size array atomically.

Cheers,

Peter.

Mime
View raw message