|
Re: volatile long: msg#00022java.jsr.166-concurrency
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: | volatile long, Hanson Char |
|---|---|
| Next by Date: | Re: volatile long, Hanson Char |
| Previous by Thread: | volatile long, Hanson Char |
| Next by Thread: | Re: volatile long, Hanson Char |
| Indexes: | [Date] [Thread] [Top] [All Lists] |
| News | FAQ | advertise |