您的位置:首页 > 其它

证明在32位hotspot上,long不是原子操作

2016-07-24 21:57 459 查看
`public class UnatomicLong implements Runnable {
private static long test = 0;

private final long val;

public UnatomicLong(long val) {
this.val = val;
}

@Override
public void run() {
while (!Thread.interrupted()) {
test = val; // 两个线程都试图将自己的私有变量val赋值给类私有静态变量test
}
}

public static void main(String[] args) {
Thread t1 = new Thread(new UnatomicLong(-1));
Thread t2 = new Thread(new UnatomicLong(0));

System.out.println(Long.toBinaryString(-1));
System.out.println(pad(Long.toBinaryString(0), 64));

t1.start();
t2.start();

long val;
while ((val = test) == -1 || val == 0) {
// 如果静态成员test的值是-1或0,说明两个线程操作没有交叉
}

System.out.println(pad(Long.toBinaryString(val), 64));
System.out.println(val);

t1.interrupt();
t2.interrupt();
}

// prepend 0s to the string to make it the target length
private static String pad(String s, int targetLength) {
int n = targetLength - s.length();
for (int x = 0; x < n; x++) {
s = "0" + s;
}
return s;
}

}`

结果为:

1111111111111111111111111111111111111111111111111111111111111111
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000011111111111111111111111111111111
4294967295
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: