网站建设资讯

NEWS

网站建设资讯

深入浅析java中volatile与lock的原理-创新互联

创新互联www.cdcxhl.cn八线动态BGP香港云服务器提供商,新人活动买多久送多久,划算不套路!

成都创新互联专注于企业全网营销推广、网站重做改版、调兵山网站定制设计、自适应品牌网站建设、H5建站商城系统网站开发、集团公司官网建设、成都外贸网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为调兵山等各大城市提供网站开发制作服务。

深入浅析java 中volatile与lock的原理?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

java 中volatile和lock原理分析

volatile和lock是Java中用于线程协同同步的两种机制。

Volatile

volatile是Java中的一个关键字,它的作用有

  • 保证变量的可见性
  • 防止重排序
  • 保证64位变量(long,double)的原子性读写
     

volatile在Java语言规范中规定的是

The Java programming language allows threads to access shared variables (§17.1). As a rule, to ensure 
that shared variables are consistently and reliably updated, a thread should ensure that it
 has exclusive use of such variables by obtaining a lock that, conventionally, enforces mutual 
exclusion for those shared variables.
The Java programming language provides a second mechanism, volatile fields, that is more convenient 
than locking for some purposes.
A field may be declared volatile, in which case the Java Memory Model ensures that all threads 
see a consistent value for the variable .
It is a compile-time error if a final variable is also declared volatile.

当前文章:深入浅析java中volatile与lock的原理-创新互联
当前网址:http://njwzjz.com/article/ccscjg.html