[concurrency-interest] Volatiles & deadlock

P.H.Welch P.H.Welch at kent.ac.uk
Wed Nov 2 10:15:48 EST 2005


Jeremy Manson wrote:
> > 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.

Nice example!

Technically, this is livelock though - an even nastier condition than
deadlock.  If theads 1 and 2 were deadlocked, they wouldn't trouble
the processor any more and their inactivity might become obvious (and
debuggable).  In this case, threads 1 and 2 continually trouble the
processor and that unproductive busyness (in a more complex embedding)
might be very un-obvious.  Volatiles scare me ...

Peter.


More information about the Concurrency-interest mailing list