[concurrency-interest] Volatiles & deadlock

Jeremy Manson jmanson at cs.purdue.edu
Wed Nov 2 09:53:17 EST 2005

Ryan LeCompte wrote:
> Is there any possible way for variables that are declared as "volatile" to
> possibly get in a deadlock by the JVM? I know that if access to a particular
> shared mutable variable is protected by methods that are "synchronized",
> that it's still possible for a deadlock to occur depending on the
> implementations of these synchronized methods. However, if there is nothing
> to worry about when declaring a variable as volatile and not using the
> "synchronized" approach to ensure correctness, right?

volatile boolean a = false;
volatile boolean b = false;

Thread 1:

1: while (!a);
2: b = true;

Thread 2:

3: while (!b);
4: a = true;

Statement 1 waits for statement 4 waits for statement 3 waits for 
statement 2 waits for statement 1: a cycle.

Of course, in this case, regular, non-volatile variables would be just 
as likely to deadlock.  Is there a case where volatiles would and normal 
variables wouldn't?  No, because volatiles are just like regular 
variables, except they have memory consistency guarantees.


More information about the Concurrency-interest mailing list