两个线程分别执行同一个类的两个方法,且这两个方法都加了 synchronized
,但是结果和预期的有出入:
class Demo
{
public long x = 0;
public Demo () { }
synchronized public void foo() {
for (int i = 0; i < 9000000; i++) {
x++;
}
}
synchronized public void bar() {
for (int i = 0; i < 9000000; i++) {
x--;
}
}
}
public class Test {
static Demo d = new Demo();
public static void main(String[] args)
{
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
d.foo();
System.out.println("线程1 "+d.x);
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
d.bar();
System.out.println("线程2 "+d.x);
}
});
t1.start();
t2.start();
try {
t1.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
t2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(d.x);
}
}
输出:
线程1 8995659
线程2 0
0
居然不是标准的 9000000
,这是怎么回事?
突然明白了……
虽然
foo
和bar
加了synchronized
,但是在foo
结束,System.out.println
输出结果时bar
就已经开始执行了,从而导致拿到的值小于预期的9000000
。