Fixed size CopyOnWriteArrayList vs array concurrent access.

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Fixed size CopyOnWriteArrayList vs array concurrent access.

evgueni

 Hi All,

We use java 1.4. We have a use case: fixed size array of objects and need concurrent access to its elements: almost even number of reads and
writes, also some iterations. The size may be less than 10 but may be more than 100.

So my choice was fixed size CopyOnWriteArrayList (COWAL) ...  but my collegues'  argument  is: when we wrie to COWAL the chunk of it is locked
while in case of array of objects we can lock just on one array element.

What is your experience/suggestion?

Many thanks,
Eugene





_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: Fixed size CopyOnWriteArrayList vs array concurrent access.

tpeierls
COWAL is most appropriate when writes are infrequent.

I think the simplest approach in your case is an array of thread-safe value-holders. You could use SynchronizedRef from Doug Lea's util.concurrent package, or AtomicReference from Dawid Kurzyniec's backport. Or just roll your own.

Note that this only deals with concurrent access of the elements as opaque values. It does not address the issue of whether the element values themselves refer to thread-safe objects; if they do not, then two threads might safely obtain the value of the first element in the array only to unsafely call methods of the object referred to by that value.

--tim

On 10/31/06, Eugene Gordienko <[hidden email]> wrote:

Hi All,

We use java 1.4. We have a use case: fixed size array of objects and need concurrent access to its elements: almost even number of reads and
writes, also some iterations. The size may be less than 10 but may be more than 100.

So my choice was fixed size CopyOnWriteArrayList (COWAL) ...  but my collegues'  argument  is: when we wrie to COWAL the chunk of it is locked
while in case of array of objects we can lock just on one array element.

What is your experience/suggestion?

Many thanks,
Eugene





_______________________________________________
Concurrency-interest mailing list
[hidden email]
<a href="http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)"> http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest


_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: Fixed size CopyOnWriteArrayList vs array concurrent access.

Brian Goetz
In reply to this post by evgueni
Your colleague has an incorrect understanding.

Because the backing array is effectively immutable (not modified after
publication), for readers it only needs to be locked briefly to
guarantee visibility of the reference the the array.  (In fact, the
implementation can do even better than that, but the point is that
multiple threads can iterate the list concurrently.)

The cost comes when you want to modify the list.  Then the list has to
be copied; if modifications are frequent, this can have significant
costs.  (In a naive implementation, newly arriving readers may be locked
out during the copy operation, but there are tricks to prevent this.)
COW structures are useful when reads greatly outnumber writes or the
size of the collection is small.

Eugene Gordienko wrote:

>  Hi All,
>
> We use java 1.4. We have a use case: fixed size array of objects and need concurrent access to its elements: almost even number of reads and
> writes, also some iterations. The size may be less than 10 but may be more than 100.
>
> So my choice was fixed size CopyOnWriteArrayList (COWAL) ...  but my collegues'  argument  is: when we wrie to COWAL the chunk of it is locked
> while in case of array of objects we can lock just on one array element.
>
> What is your experience/suggestion?
>
> Many thanks,
> Eugene
>
>
>
>
>
> _______________________________________________
> Concurrency-interest mailing list
> [hidden email]
> http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest