Volatile variable is used for communicating between threads so that value can be updated atomically and all thread can see same value at same time .
Real time use -
Suppose we have to stop application's all threads after pressing stop button . create "public static volatile boolean " flag and check that flag in each thread .Once value of that flag changes to "true" after pressing button all threads will see it changed and they will quit as they check that flag say in loop .
volatile here is for atomic update of value .Means all threads will see changes value not corrupted one.All threads see same copy of data .Means it avoid to show past value to one thread and new value to another .So it is effective to pass value between threads and updates are atomic to all threads .
We can say it is lightweight synchronization .
Following code is just example but will not create corrupted value of volatile int :)
Volatile has happen before relationship with other thread means updated value is guaranteed to be displayed to all other threads .
In java long and double are not tread safe as they are constructed using other small types like group of bytes it involves two steps writing first half part then second half .So this operation of two step is not atomic .
But volatile double and volatile long are guaranteed to be happen before and atomic so be cautious while using long and double in multi-threaded environments .
Real time use -
Suppose we have to stop application's all threads after pressing stop button . create "public static volatile boolean " flag and check that flag in each thread .Once value of that flag changes to "true" after pressing button all threads will see it changed and they will quit as they check that flag say in loop .
volatile here is for atomic update of value .Means all threads will see changes value not corrupted one.All threads see same copy of data .Means it avoid to show past value to one thread and new value to another .So it is effective to pass value between threads and updates are atomic to all threads .
We can say it is lightweight synchronization .
Following code is just example but will not create corrupted value of volatile int :)
Volatile has happen before relationship with other thread means updated value is guaranteed to be displayed to all other threads .
In java long and double are not tread safe as they are constructed using other small types like group of bytes it involves two steps writing first half part then second half .So this operation of two step is not atomic .
But volatile double and volatile long are guaranteed to be happen before and atomic so be cautious while using long and double in multi-threaded environments .
package com.salpe.basic.Threads;
import java.util.Random;
public class TestThread {
public static volatile long test = 0;
public static Random randomGenerator = new Random();
public static void main(String[] args) {
Thread th1 = new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName() + " Seen as " +test);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
Thread th2 = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
// TODO Auto-generated method stub
test = randomGenerator.nextInt();
System.out.println(Thread.currentThread().getName() + " Changed to " +test);
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
th1.start();
th2.start();
}
}
No comments:
Post a Comment