volatile reference vs. AtomicReference

View: New views
7 Messages — Rating Filter:   Alert me  

volatile reference vs. AtomicReference

by Peter Kovacs-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

I've been wondering whether it makes sense to use AtomicReference
instead of a volatile reference, if I only call the get() and set()
method of AtomicReference. Or is it that reference assignment is not
atomic in 32-bit JVMs?

Thanks
Peter
_______________________________________________
Concurrency-interest mailing list
Concurrency-interest@...
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest

Re: volatile reference vs. AtomicReference

by Matthias Ernst :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

There's no difference IMHO.

On Thu, Jul 3, 2008 at 10:37 AM, Peter Kovacs
<peter.kovacs.1.0rc@...> wrote:

> Hi,
>
> I've been wondering whether it makes sense to use AtomicReference
> instead of a volatile reference, if I only call the get() and set()
> method of AtomicReference. Or is it that reference assignment is not
> atomic in 32-bit JVMs?
>
> Thanks
> Peter
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest@...
> http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
>
_______________________________________________
Concurrency-interest mailing list
Concurrency-interest@...
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest

Re: volatile reference vs. AtomicReference

by Mallikarjunaiah, Praveena :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Internally, AtomicReference uses volatile reference itself

Thanks
Praveen

-----Original Message-----
From: concurrency-interest-bounces@...
[mailto:concurrency-interest-bounces@...] On Behalf Of Peter
Kovacs
Sent: Thursday, July 03, 2008 2:07 PM
To: concurrency-interest
Subject: [concurrency-interest] volatile reference vs. AtomicReference

Hi,

I've been wondering whether it makes sense to use AtomicReference
instead of a volatile reference, if I only call the get() and set()
method of AtomicReference. Or is it that reference assignment is not
atomic in 32-bit JVMs?

Thanks
Peter
_______________________________________________
Concurrency-interest mailing list
Concurrency-interest@...
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest

_______________________________________________
Concurrency-interest mailing list
Concurrency-interest@...
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest

Re: volatile reference vs. AtomicReference

by Tom Hawtin :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Peter Kovacs wrote:
>
> I've been wondering whether it makes sense to use AtomicReference
> instead of a volatile reference, if I only call the get() and set()
> method of AtomicReference. Or is it that reference assignment is not
> atomic in 32-bit JVMs?

If you use a volatile reference, then it is one less object. OTOH, your
code might be more consistent if you use always AtomicReference in place
of volatile, and if you find you need one of the other methods the
changes is easier. (Although there is AtomicReferenceFieldUpdater.)

Tom
_______________________________________________
Concurrency-interest mailing list
Concurrency-interest@...
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest

Re: volatile reference vs. AtomicReference

by David Holmes-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Reference assignment is always atomic.

I wouldn't bother with AtomicReference unless I needed the CAS. But it's a
stylistic choice more than anything.

David Holmes

> -----Original Message-----
> From: concurrency-interest-bounces@...
> [mailto:concurrency-interest-bounces@...]On Behalf Of Peter
> Kovacs
> Sent: Thursday, 3 July 2008 6:37 PM
> To: concurrency-interest
> Subject: [concurrency-interest] volatile reference vs. AtomicReference
>
>
> Hi,
>
> I've been wondering whether it makes sense to use AtomicReference
> instead of a volatile reference, if I only call the get() and set()
> method of AtomicReference. Or is it that reference assignment is not
> atomic in 32-bit JVMs?
>
> Thanks
> Peter
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest@...
> http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest

_______________________________________________
Concurrency-interest mailing list
Concurrency-interest@...
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest

Parent Message unknown Re: volatile reference vs. AtomicReference

by Ben Manes :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.
I prefer AtomicReference instead of volatile simply because it is more obvious to other developers and they can quickly look at the JavaDoc.  That means that if someone who is looking at my code and does not work regularly with threads, it shouldn't be a surprise.  Most Java developers I've met don't quite understand volatile, so its one less thing to confuse them.  Pretty much stylistic, as David said, since they perform comparably...

----- Original Message ----
From: David Holmes <dcholmes@...>
To: concurrency-interest <Concurrency-interest@...>
Sent: Thursday, July 3, 2008 4:49:42 AM
Subject: Re: [concurrency-interest] volatile reference vs. AtomicReference

Reference assignment is always atomic.

I wouldn't bother with AtomicReference unless I needed the CAS. But it's a
stylistic choice more than anything.

David Holmes

> -----Original Message-----
> From: concurrency-interest-bounces@...
> [mailto:concurrency-interest-bounces@...]On Behalf Of Peter
> Kovacs
> Sent: Thursday, 3 July 2008 6:37 PM
> To: concurrency-interest
> Subject: [concurrency-interest] volatile reference vs. AtomicReference
>
>
> Hi,
>
> I've been wondering whether it makes sense to use AtomicReference
> instead of a volatile reference, if I only call the get() and set()
> method of AtomicReference. Or is it that reference assignment is not
> atomic in 32-bit JVMs?
>
> Thanks
> Peter
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest@...
> http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest

_______________________________________________
Concurrency-interest mailing list
Concurrency-interest@...
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest


_______________________________________________
Concurrency-interest mailing list
Concurrency-interest@...
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest

Re: volatile reference vs. AtomicReference

by Peter Kovacs-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

An interesting (and useful) consideration. ("Self-documenting code" is
one of my favorite themes, but somehow forgot about it in this case.)

Thank you all for your answers.

Peter

On Thu, Jul 3, 2008 at 10:57 PM, Ben Manes <ben_manes@...> wrote:

> I prefer AtomicReference instead of volatile simply because it is more
> obvious to other developers and they can quickly look at the JavaDoc.  That
> means that if someone who is looking at my code and does not work regularly
> with threads, it shouldn't be a surprise.  Most Java developers I've met
> don't quite understand volatile, so its one less thing to confuse them.
> Pretty much stylistic, as David said, since they perform comparably...
>
> ----- Original Message ----
> From: David Holmes <dcholmes@...>
> To: concurrency-interest <Concurrency-interest@...>
> Sent: Thursday, July 3, 2008 4:49:42 AM
> Subject: Re: [concurrency-interest] volatile reference vs. AtomicReference
>
> Reference assignment is always atomic.
>
> I wouldn't bother with AtomicReference unless I needed the CAS. But it's a
> stylistic choice more than anything.
>
> David Holmes
>
>> -----Original Message-----
>> From: concurrency-interest-bounces@...
>> [mailto:concurrency-interest-bounces@...]On Behalf Of Peter
>> Kovacs
>> Sent: Thursday, 3 July 2008 6:37 PM
>> To: concurrency-interest
>> Subject: [concurrency-interest] volatile reference vs. AtomicReference
>>
>>
>> Hi,
>>
>> I've been wondering whether it makes sense to use AtomicReference
>> instead of a volatile reference, if I only call the get() and set()
>> method of AtomicReference. Or is it that reference assignment is not
>> atomic in 32-bit JVMs?
>>
>> Thanks
>> Peter
>> _______________________________________________
>> Concurrency-interest mailing list
>> Concurrency-interest@...
>> http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
>
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest@...
> http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
>
>
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest@...
> http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
>
>
_______________________________________________
Concurrency-interest mailing list
Concurrency-interest@...
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest