[英]java concurrency issue
为什么n
有时等于1或2
private static int n = 0;
private static Thread t1, t2;
private synchronized static void increment() {
n++;
}
public static void main(String[] args) {
t1 = new Thread(new Runnable() {
public void run() {
increment();
}
});
t2 = new Thread(new Runnable() {
public void run() {
t1.start();
increment();
}
});
t2.start();
try {
t2.join();
} catch(InterruptedException e) {
e.printStackTrace();
}
System.out.println(n);
}
增量方法不应该只允许一个线程在任何给定时刻执行它吗?
也许它是调试器,似乎当我正常运行时我总是得到2但是当我调试代码时它有时会返回1。
确实如此,它可能以任何顺序发生。 你只需等待t2
完成,但不能等待t1
完成。
等待t1
。
private static int n = 0;
private static Thread t1, t2;
private synchronized static void increment() { // Lock will be on the "class" Object
n++;
}
public static void main(String[] args) {
t1 = new Thread(new Runnable() {
public void run() {
increment();
}
});
t2 = new Thread(new Runnable() {
public void run() {
t1.start();
// t1 starts after t2. Now, t1's increment might also be called or t2's increment() might also be called. If t2 calls increment(), then the join() method below (you are joining the in the main thread) will be completed and "n" will be printed (if t1 is not getting appropriate time of execution..)
increment();
}
});
t2.start(); // t2 starts first
try {
t2.join();
} catch(InterruptedException e) {
e.printStackTrace();
}
System.out.println(n); // increment() might not have been called by t1
}
无法保证一个线程会在另一个线程之前执行(即使具有同步条件..)。 所以,你可以join
t1和t2。 这将确保您始终输出为2。
我想,如果increment
中调用run
的方法t2
发生的前increment
的方法t1
在被调用t1
的run
方法,然后n
被锁定,由时间t2
结束, t1
可能仍在运行,但你在t1
的增量结束之前,打印输出n
增加t2
。
澄清:
t2
开始了 t2
产生t1
t2
在t1
有机会之前调用increment
n
在t2
的increment
持续时间内被锁定 Thread
在t1
有机会increment
之前加入t2
Thread
在t1
有机会increment
之前打印n
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.