Java中volatile的用处

Java中volatile的用处

今天由于要写一个线程进程,无意中看到了volatile的用法,觉得非常有用。
 
一般的,如果多个线程协作存、取某个变量时,一般需要用到synchronized关键字进行同步操作,如:
public class MyTestThread extends MyTest implements Runnable {
    private boolean _done = false;
    public synchronized boolean getDone()
    {
        return _done;
    }
    public synchronized void setDone(boolean b)
    {
        _done = b;
    }
   
    public void run( ) {
        boolean done;
        done = getDone();
        while (!done) {
            repaint( );
            try {
                Thread.sleep(100);
            } catch (InterruptedException ie) {
                return;
            }
        }
    }
}
或者:
public class MyTestThread extends MyTest implements Runnable {
    private boolean _done = false;
    public void setDone(boolean b)
    {
        synchronized(this)
        {
            _done = b;
        }
    }
   
    public void run( ) {
        boolean done;
        synchronized(this)
        {
            done = _done;
        }
       
        while (!done) {
            repaint( );
            try {
                Thread.sleep(100);
            } catch (InterruptedException ie) {
                return;
            }
        }
    }
}
但是,通过volatile关键字,我们可以大大简化:
public class MyTestThread extends MyTest implements Runnable {
    private volatile boolean done = false;
    public void run( ) {
        while (!done) {
            repaint( );
            try {
                Thread.sleep(100);
            } catch (InterruptedException ie) {
                return;
            }
        }
    }
    public void setDone(boolean b) {
        done = b;
    }
}
相关的介绍如下:
Java specifies that basic loading and storing of variables (except for long and double variables) is atomic. That means the value of the variable can't be found in an interim state during the store, nor can it be changed in the middle of loading the variable to a register. The setDone() method has only one store operation; therefore, it is atomic.
Unfortunately, Java's memory model is a bit more complex. Threads are allowed to hold the values of variables in local memory (e.g., in a machine register). In that case, when one thread changes the value of the variable, another thread may not see the changed variable. This is particularly true in loops that are controlled by a variable (like the done flag that we are using to terminate the thread): the looping thread may have already loaded the value of the variable into a register and does not necessarily notice when another thread changes the variable.
One way to solve this problem is to provide setter and getter methods for the variable. We can then simply synchronize access by using the synchronized keyword on these methods.
However, Java provides a more elegant solution: the volatile keyword. If a variable is marked as volatile, every time the variable is used it must be read from main memory. Similarly, every time the variable is written, the value must be stored in main memory.

你可能感兴趣的:(Java中volatile的用处)