|
Re: volatile long: msg#00025java.jsr.166-concurrency
> each is composed of a read followed by a write. I think this is false. Had the statements been something like: if (count++) ... Then it would be a read followed by a write. But just count++ only involves a write. Hanson On Thu, 17 Feb 2005 01:10:43 -0800, Joe Bowbeer <jozart@xxxxxxxxx> wrote: > Even though though ++ and -- are each represented by a single bytecode > (iinc), I don't think these are atomic operations in terms of the JMM. That > is, each is composed of a read followed by a write. In which case, even an > "int" count can creep very negative or very positive over time, depending on > the interleaving of threads. Use AtomicInteger to prevent counter creep. > > Volatile protects long values from word shearing, but it doesn't turn ++ > or -- into atomic operations either. > > > ----- Original Message ----- > From: "Hanson Char" <hanson.char@xxxxxxxxx> > To: <concurrency-interest@xxxxxxxxxxxxxxxxxxxx> > Sent: Thursday, February 17, 2005 12:49 AM > Subject: [concurrency-interest] volatile long > > A question on the use of volatile in a concurrent environment. > Consider the example: > > public class Foo { > private volatile int count; > > public void run() { > count++; > // ... do some other operations > count--; > } > public int getCount() { return count; } > } > > My understanding is: > > 1) The value returned by getCount() will never be less than zero; and > 2) count will never be in a corrupted state. > > Does the above 2 statements still hold if count is type long instead of int > ? > > Hanson > _______________________________________________ > Concurrency-interest mailing list > Concurrency-interest@xxxxxxxxxxxxxxxxxxxx > http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest > >
|
|
| <Prev in Thread] | Current Thread | [Next in Thread> |
|---|---|---|
| Previous by Date: | Re: volatile long, Hanson Char |
|---|---|
| Next by Date: | Re: volatile long, Joe Bowbeer |
| Previous by Thread: | RE: volatile long, David Holmes |
| Next by Thread: | Why is ReentrantLock faster than synchronized ?, Ernst, Matthias |
| Indexes: | [Date] [Thread] [Top] [All Lists] |
| News | FAQ | advertise |