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.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值