volatile Fields(langspec-3.0)
来源:互联网 发布:淘宝店和快递公司 编辑:程序博客网 时间:2024/06/15 01:57
As described in §17, the Java programming language allows threads to access
shared variables. 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
(§17) ensures that all threads see a consistent value for the variable.
If, in the following example, one thread repeatedly calls the method one (but
no more than Integer.MAX_VALUE times in all), and another thread repeatedly
calls the method two:
class Test {
static int i = 0, j = 0;
static void one() { i++; j++; }
static void two() {
System.out.println("i=" + i + " j=" + j);
}
}
then method two could occasionally print a value for j that is greater than the
value of i, because the example includes no synchronization and, under the rules
explained in §17, the shared values of i and j might be updated out of order.
One way to prevent this out-or-order behavior would be to declare methods
one and two to be synchronized (§8.4.3.6):
class Test {
static int i = 0, j = 0;
static synchronized void one() { i++; j++; }
static synchronized void two() {
System.out.println("i=" + i + " j=" + j);
}
}
This prevents method one and method two from being executed concurrently, and
furthermore guarantees that the shared values of i and j are both updated before
method one returns. Therefore method two never observes a value for j greater
than that for i; indeed, it always observes the same value for i and j.
Another approach would be to declare i and j to be volatile:
class Test {
static volatile int i = 0, j = 0;
static void one() { i++; j++; }
static void two() {
System.out.println("i=" + i + " j=" + j);
}
}
This allows method one and method two to be executed concurrently, but
guarantees that accesses to the shared values for i and j occur exactly as many
times, and in exactly the same order, as they appear to occur during execution of
the program text by each thread. Therefore, the shared value for j is never greater
than that for i, because each update to i must be reflected in the shared value for
i before the update to j occurs. It is possible, however, that any given invocation
of method two might observe a value for j that is much greater than the value
observed for i, because method one might be executed many times between the
moment when method two fetches the value of i and the moment when method
two fetches the value of j.
See §17 for more discussion and examples.
A compile-time error occurs if a final variable is also declared volatile.
最后一条,在多线程的情况下,就经常会出现i比j小。
加锁机制既可以确保可见性又可以确保原子性,而volatile变量只能确保可见性。
- volatile Fields(langspec-3.0)
- Volatile Fields
- 学习 java langspec-3.0. 规范后的代码
- Volatile fields in .NET: A look inside
- Text Fields(文本框)
- 高级属性(Advanced Fields)
- POJ3254 Corn Fields(DP)
- 感受野(receptive fields)
- Corn Fields (状态dp)
- POJ3254 Corn Fields(状态压缩)
- poj3254 Corn Fields (状态压缩dp)
- POJ 3254 Corn Fields(状压dp)
- poj 2226 Muddy Fields(二分图)
- 条件随机场(Conditional random fields)
- POJ 3254 Corn Fields(状态压缩)
- poj3254(Corn Fields)状压dp
- 条件随机场(Conditional random fields)
- POJ 3254 Corn Fields(状态压缩)
- 2012年个人规划(2012/2/8)
- 无聊中~~看《失恋33天》,单片循环
- IT新兵成长记-(一)
- 打造百折不挠的 setuptools
- Hash 算法及其应用(转)
- volatile Fields(langspec-3.0)
- Using the 2.6.33-rt kernel series on Ubuntu Lucid
- futex pthread_mutex_lock 相关故障排查
- 在ListView中绑定并显示数据
- FPGA实现任意分频 为所欲为——教你什么才是真正的任意分频
- Asp.net MVC 学习笔记
- Vulnerable by Design
- android横竖屏总结
- form,function,菜单常用sql