Stv*_*dll 5 java concurrency multithreading synchronized synchronized-block
下面是一个简单的java程序.它有一个名为"cnt"的计数器,它会递增,然后添加到名为"monitor"的List中."cnt"由多个线程递增,并且值被多个线程添加到"监视器".
在方法"go()"的末尾,cnt和monitor.size()应该具有相同的值,但它们不具有相同的值.monitor.size()确实有正确的值.
如果通过取消注释其中一个已注释的同步块来更改代码,并注释掉当前未注释的块,则代码会生成预期结果.此外,如果将线程计数(THREAD_COUNT)设置为1,则代码会生成预期结果.
这只能在具有多个真实核心的计算机上重现.
public class ThreadTester {
private List<Integer> monitor = new ArrayList<Integer>();
private Integer cnt = 0;
private static final int NUM_EVENTS = 2313;
private final int THREAD_COUNT = 13;
public ThreadTester() {
}
public void go() {
Runnable r = new Runnable() {
@Override
public void run() {
for (int ii=0; ii<NUM_EVENTS; ++ii) {
synchronized( monitor) {
synchronized(cnt) { // <-- is this synchronized necessary?
monitor.add(cnt);
}
// synchronized(cnt) {
// cnt++; // <-- why does moving the synchronized block to here result in the correct value for cnt?
// }
}
synchronized(cnt) {
cnt++; // <-- why does moving the synchronized block here result in cnt being wrong?
}
}
// synchronized(cnt) {
// cnt += NUM_EVENTS; // <-- moving the synchronized block here results in the correct value for cnt, no surprise
// }
}
};
Thread[] threads = new Thread[THREAD_COUNT];
for (int ii=0; ii<THREAD_COUNT; ++ii) {
threads[ii] = new Thread(r);
}
for (int ii=0; ii<THREAD_COUNT; ++ii) {
threads[ii].start();
}
for (int ii=0; ii<THREAD_COUNT; ++ii) {
try { threads[ii].join(); } catch (InterruptedException e) { }
}
System.out.println("Both values should be: " + NUM_EVENTS*THREAD_COUNT);
synchronized (monitor) {
System.out.println("monitor.size() " + monitor.size());
}
synchronized (cnt) {
System.out.println("cnt " + cnt);
}
}
public static void main(String[] args) {
ThreadTester t = new ThreadTester();
t.go();
System.out.println("DONE");
}
}
Run Code Online (Sandbox Code Playgroud)
好吧,让我们看看您提到的不同可能性:
1.
for (int ii=0; ii<NUM_EVENTS; ++ii) {
synchronized( monitor) {
synchronized(cnt) { // <-- is this synchronized necessary?
monitor.add(cnt);
}
synchronized(cnt) {
cnt++; // <-- why does moving the synchronized block to here result in the correct value for cnt?
}
}
Run Code Online (Sandbox Code Playgroud)
首先,监视器对象在线程之间共享,因此获得它的锁(这就是同步的作用)将确保块内的代码一次只能由一个线程执行。因此,外部同步内部的 2 个同步是不必要的,无论如何,代码都受到保护。
2.
for (int ii=0; ii<NUM_EVENTS; ++ii) {
synchronized( monitor) {
monitor.add(cnt);
}
synchronized(cnt) {
cnt++; // <-- why does moving the synchronized block here result in cnt being wrong?
}
}
Run Code Online (Sandbox Code Playgroud)
好吧,这个有点棘手。cnt 是一个 Integer 对象,Java 不允许修改 Integer 对象(整数是不可变的),尽管代码表明这就是这里发生的情况。但实际上会发生的是 cnt++ 将创建一个值为 cnt + 1 的新 Integer 并覆盖 cnt。这就是代码的实际作用:
synchronized(cnt) {
Integer tmp = new Integer(cnt + 1);
cnt = tmp;
}
Run Code Online (Sandbox Code Playgroud)
问题是,虽然一个线程将创建一个新的 cnt 对象,但所有其他线程都在等待获取旧对象的锁。该线程现在释放旧的 cnt,然后尝试获取新的 cnt 对象的锁,并在另一个线程获取旧的 cnt 对象的锁时获取它。突然有 2 个线程进入临界区,执行相同的代码并导致竞争条件。这就是错误结果的来源。
如果您删除第一个同步块(带有监视器的同步块),那么您的结果会变得更加错误,因为竞争的机会会增加。
一般来说,您应该尝试仅对最终变量使用同步,以防止这种情况发生。