Re: volatile
- From: "Timo Nentwig" <timo.nentwig@xxxxxxxxx>
- Date: 31 Mar 2006 08:23:14 -0800
Timo Stamm wrote:
| The volatile modifier requests the Java VM to always access the shared
| copy of the variable so the its most current value is always read. If
| two or more threads access a member variable, AND one or more threads
| might change that variable's value, AND ALL of the threads do not use
| synchronization (methods or blocks) to read and/or write the value,
| then that member variable must be declared volatile to ensure all
| threads see the changed value.
Yes...
Okay, some shorter pseudo-code:
thread1{
int value;
void modify(){ value++; }
void print() {System.out.println( value ); }
}
thread2{
thread1.modify();
// IMHO I don't need volatile here:
thread1.print();
// but I wouldneed it here:
System.out.println(thread1.value);
}
.
- Follow-Ups:
- Re: volatile
- From: Timo Stamm
- Re: volatile
- From: Timo Nentwig
- Re: volatile
- References:
- volatile
- From: Timo Nentwig
- Re: volatile
- From: Timo Stamm
- volatile
- Prev by Date: Re: How to stop at the crash source statement when debugging with Eclipse ?
- Next by Date: Re: Reading HDD serialNo
- Previous by thread: Re: volatile
- Next by thread: Re: volatile
- Index(es):