Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #11136
| Date | 2012-01-09 10:56 -0800 |
|---|---|
| From | Patricia Shanahan <pats@acm.org> |
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Atomic integer array class question |
| References | <9d7d89a6-3109-412f-a898-e2c9e82b780b@cs7g2000vbb.googlegroups.com> <jef22t$oc9$1@dont-email.me> <803273c0-b874-4d53-a9f7-70cbefac5ddd@k5g2000vba.googlegroups.com> |
| Message-ID | <L4SdnYBDN4fhpZbSnZ2dnUVZ_gudnZ2d@earthlink.com> (permalink) |
On 1/9/2012 8:17 AM, raphfrk@gmail.com wrote:
> On Jan 9, 3:45 pm, Knute Johnson<nos...@knutejohnson.com> wrote:
>> Read the package description for java.util.concurrent.atomic to get a
>> good idea of what the Atomic variables are. But no the performance will
>> not be as good as a regular array.
>
> Sorry, I wasn't clear, I didn't mean compared to a regular array.
>
> AtomicIntegerArray intArray = new AtomicIntegerArray(10);
>
> compared to
>
> AtomicInteger[] intArray = new AtomicInteger[10];
>
> If 2 writes happen to an AtomicIntegerArray, but at different
> locations, do they interfere?
>
>> Why don't you post something about what you are really going to do with
>> the array and maybe somebody can give you a better idea of what way to go.
>
> I was wondering if having writes to an array happen one at a time
> would be slower than an AtomicIntegerArray.
I don't understand this comment. Each AtomicIntegerArray operation
refers to a specific element, so writes must happen one at a time anyway.
>
> Something like (though not actually for int arrays, as then I would
> just use AtomicIntegerArray)
>
> private final int UNSTABLE = 0;
> int[] array = new int[1000];
> AtomicInteger sequence = new AtomicInteger(1);
>
> public void set(int index, int value) {
> while (true) {
> int oldSequence = sequence.getAndSet(UNSTABLE);
> if (oldSequence == UNSTABLE) {
> continue;
> }
Spin waiting, especially spin waiting that has no delay and includes
access to a volatile variable, can be very expensive in terms of load on
the processor-memory interconnect.
I would do some benchmarking, but are you sure this is better than
synchronization?
> array[index] = value;
> sequence.set(oldSequence + 2);
> return;
> }
> }
>
> public void get(int index) {
> while (true) {
> int initialSequence = sequence.get();
> if (localSequence == UNSTABLE) {
> continue;
> }
> int value = array[index];
> int finalSequence = sequence.get();
> if (initialSequence != finalSequence) {
> continue;
> }
> return value;
> }
> }
>
> So, the effect is that writes happen one at a time. If a thread sets
> the sequence number to UNSTABLE, then all other writers will
> spinlock. Also, if any element is changed, then all readers will
> detect it and have to retry (even though their data was actually not
> changed).
I'm still unclear what is intent, and what is infrastructure to do with
how you are trying to implement the intent. Could you provide a
description of what you want to have happen?
Patricia
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Atomic integer array class question "raphfrk@gmail.com" <raphfrk@gmail.com> - 2012-01-09 07:10 -0800
Re: Atomic integer array class question Knute Johnson <nospam@knutejohnson.com> - 2012-01-09 07:45 -0800
Re: Atomic integer array class question "raphfrk@gmail.com" <raphfrk@gmail.com> - 2012-01-09 08:17 -0800
Re: Atomic integer array class question Patricia Shanahan <pats@acm.org> - 2012-01-09 10:56 -0800
Re: Atomic integer array class question "raphfrk@gmail.com" <raphfrk@gmail.com> - 2012-01-10 06:22 -0800
Re: Atomic integer array class question Roedy Green <see_website@mindprod.com.invalid> - 2012-01-10 06:52 -0800
Re: Atomic integer array class question Lew <noone@lewscanon.com> - 2012-01-10 07:17 -0800
Re: Atomic integer array class question Patricia Shanahan <pats@acm.org> - 2012-01-10 07:43 -0800
Re: Atomic integer array class question markspace <-@.> - 2012-01-10 08:37 -0800
csiph-web