Mixing volatile and synchronized together...

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

Mixing volatile and synchronized together...

Ryan LeCompte

Hello all,

 

I know that there really isn’t a need to do this, but I was wondering if the following code snippet would be “safe” – ie, no deadlocks…

 

class Test {

   private volatile boolean value = false;

 

    synchronized void someOperation() {

        // do something with “value”

     }

}

 

In other words, there is no harm in using a volatile variable in a synchronized method or block, right?

 

Thanks,

Ryan

 


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

Re: Mixing volatile and synchronized together...

Jeremy Manson
Ryan LeCompte wrote:

> class Test {
>
>    private volatile boolean value = false;
>
>     synchronized void someOperation() {
>
>         // do something with "value"
>
>      }
>
> }
>
> In other words, there is no harm in using a volatile variable in a
> synchronized method or block, right?
>
>  

Right.

Volatiles can actually be used anywhere fields with no modifiers can be
used.  It would actually be semantically harmless to mark every field in
your program volatile, if you wanted (BAD IDEA).  The program's results
should be the same, for correctly written code.  The reasons it is a bad
idea are: it would decrease performance, it would damage readability,
and it probably wouldn't buy you the guarantees that you think it would
buy you.

Now, as regards your case: it should be said that if you are using
locking everywhere the volatile is accessed, then the volatile isn't
going to provide additional assurances.  For this pattern to be useful,
there would have to be another thread that accesses value without blocking.

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

Re: Mixing volatile and synchronized together...

tpeierls
In reply to this post by Ryan LeCompte
Ryan LeCompte wrote:
> I know that there really isn’t a need to do this, but I was wondering if
> In other words, there is no harm in using a volatile variable in a
> synchronized method or block, right?

Right, no harm. Depending on the situation, one or the other might be superfluous, but there is
nothing unsafe about accessing a volatile in a synchronized block or method.

--tim



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

RE: Mixing volatile and synchronized together...

Ryan LeCompte
Great. I just wanted to be clear on the behavior. So it's okay then to use a
volatile variable in a synchronized method/block as well as an
unsynchronized method/block in the same class?

Thanks,
Ryan

-----Original Message-----
From: Tim Peierls [mailto:[hidden email]]
Sent: Friday, October 14, 2005 11:38 AM
To: Ryan LeCompte
Cc: [hidden email]
Subject: Re: [concurrency-interest] Mixing volatile and synchronized
together...

Ryan LeCompte wrote:
> I know that there really isn't a need to do this, but I was wondering if
> In other words, there is no harm in using a volatile variable in a
> synchronized method or block, right?

Right, no harm. Depending on the situation, one or the other might be
superfluous, but there is
nothing unsafe about accessing a volatile in a synchronized block or method.

--tim

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

Re: Mixing volatile and synchronized together...

tpeierls
Ryan LeCompte wrote:
> Great. I just wanted to be clear on the behavior. So it's okay then to use a volatile variable
> in a synchronized method/block as well as an unsynchronized method/block in the same class?

Yes. It's worth reading Jeremy Manson's response carefully:

Jeremy Manson wrote:

> Volatiles can actually be used anywhere fields with no modifiers can be used.  It would
> actually be semantically harmless to mark every field in your program volatile, if you wanted
> (BAD IDEA).  The program's results should be the same, for correctly written code.  The reasons
> it is a bad idea are: it would decrease performance, it would damage readability, and it
> probably wouldn't buy you the guarantees that you think it would buy you.
>
> Now, as regards your case: it should be said that if you are using locking everywhere the
> volatile is accessed, then the volatile isn't going to provide additional assurances.  For this
> pattern to be useful, there would have to be another thread that accesses value without
> blocking.


--tim

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

Re: Mixing volatile and synchronizedtogether...

Dawid Kurzyniec
In reply to this post by Ryan LeCompte
Ryan LeCompte wrote:

>Great. I just wanted to be clear on the behavior. So it's okay then to use a
>volatile variable in a synchronized method/block as well as an
>unsynchronized method/block in the same class?
>
>Thanks,
>Ryan
>
>-----Original Message-----
>From: Tim Peierls [mailto:[hidden email]]
>Sent: Friday, October 14, 2005 11:38 AM
>To: Ryan LeCompte
>Cc: [hidden email]
>Subject: Re: [concurrency-interest] Mixing volatile and synchronized
>together...
>
>Ryan LeCompte wrote:
>  
>
>>I know that there really isn't a need to do this, but I was wondering if
>>In other words, there is no harm in using a volatile variable in a
>>synchronized method or block, right?
>>    
>>
Yes, but keep in mind that a volatile (unsynchronized) reader will be
able to see any value set in the middle of a synchronized block by any
writer, so it is only OK if the volatile does not participate in a
multi-field state invariant, and if there are no intermittent illegal
values set temporarily to the variable inside synchronized blocks. In
other words, it must be OK for the reader to see any value that can be
possibly assigned anytime, including in the middle of the synchronized
block, by any writer.

Good example: emulating atomic variables in Java 1.4: all writer methods
are synchronized, but get() is a volatile-read.

Regards,
Dawid

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest