Ciao

se non ho capito male quando ci sono 1 o più thread (single o multi trhead) appena una variable dichiarata di tipo volatile viene letta da tutti questi viene fatto in modo che l'ultima modifica al valore della variabile appaia a tutti e che tutti quindi leggano quell'ultimo valore..ho trovato questo esempio che di volatile ha solo una variabile booleana missedIt mentre la variabile intera value (sempre nel metodo run) non è stata dischiarata apposta volatile. Tale variabile modificata dal trhead principale "main" nel metodo workflow il cui valore da 10 passa a 60, viene cmq letta dal while del metodo run nel valore apena aggiornato facendo terminare il metodo run quando se non ho capito male non essendo volatile il valore vecchio dovrebbe rimanere e solamente la variabile booleana missedIt dovrebbe apparire aggiornata a true e far stampare print("in run() - see value=" + currValue
+ ", but rumor has it that it changed!");
print("in run() - valueAfterSync=" + valueAfterSync);


l'esempio di cui parlo è qua sotto, dato che è poco identato lo trovate + ordinato al
link: http://www.java2s.com/Code/Java/Thre...evariables.htm

se la print da errore l'ho modificata qua

il mio output è:

61 Thread-0: entering run()
91 main: entering workMethod()
91 main: about to sleep for 2 seconds
94 main: just set value=60
94 main: about to sleep for 5 seconds
--->>>> 94 Thread-0: leaving run() with value = 60 (ma value non è volatile nn dovrebbe essere per il thread 10 ancora???)
01 main: just set missedIt=true (ok questa l'ha letta giusta dopo che è stata modificata)
01 main: about to sleep for 3 seconds
05 main: leaving workMethod()

public class Volatile implements Runnable {

// not marked as 'volatile', but it should be!
private int value;

private volatile boolean missedIt;

// doesn't need to be volatile-doesn't change
private long creationTime;

public Volatile() {
value = 10;
missedIt = false;
creationTime = System.currentTimeMillis();
}

public void run() {
print("entering run()");

// each time, check to see if 'value' is different
while (value < 20) {
// Used to break out of the loop if change to
// value is missed.
if (missedIt) {
int currValue = value;

// Simply execute a synchronized statement on an
// arbitrary object to see the effect.
Object lock = new Object();
synchronized (lock) {
// do nothing!
print("i'm in block");
}

int valueAfterSync = value;

print("in run() - see value=" + currValue
+ ", but rumor has it that it changed!");
print("in run() - valueAfterSync=" + valueAfterSync);

break;
}
}

print("leaving run() with value = " + value);
}

public void workMethod() throws InterruptedException {
print("entering workMethod()");

print("about to sleep for 2 seconds");
Thread.sleep(2000);

value = 60;
print("just set value=" + value);

print("about to sleep for 5 seconds");
Thread.sleep(5000);

missedIt = true;
print("just set missedIt=" + missedIt);

print("about to sleep for 3 seconds");
Thread.sleep(3000);

//value = 50;
//print("just set value=" + value);

print("leaving workMethod()");
}

private void s(String msg){
System.out.println(msg);
}
private void s(long msg){
System.out.println(msg);
}


private void print(String msg) {

long interval = System.currentTimeMillis() - creationTime;


String tmpStr = (interval / 1000.0) + "000";


int pos = tmpStr.indexOf(".");

//s(pos);

String secStr = tmpStr.substring(pos + 2, pos + 4);

String nameStr = Thread.currentThread().getName();

nameStr = nameStr.substring(0/*length()*- 8*/, nameStr.length());

System.out.println(secStr + " " + nameStr + ": " + msg);
}

public static void main(String[] args) {
try {
Volatile vol = new Volatile();

// slight pause to let some time elapse
Thread.sleep(100);

Thread t = new Thread(vol);
t.start();

// slight pause to allow run() to go first
Thread.sleep(100);

vol.workMethod();
} catch (InterruptedException x) {
System.err.println("one of the sleeps was interrupted");
}
}

}