同步奇怪的行为
synchronization weird behaviour
我在刷我的多线程基础。为了理解同步,我创建了以下程序-
package thread;
public class SynchronizedCounter implements Runnable{
private static int counter = 0;
public void run() {
while(counter < 10) {
synchronized (SynchronizedCounter.class) {
System.out.println(Thread.currentThread().getName() + " :: reads ## " + counter);
counter++;
System.out.println(Thread.currentThread().getName() + " :: updated value ## " + counter);
}
}
}
public static void main(String[] args) {
Thread[] threads = new Thread[5];
for(int i = 0; i < threads.length; i++) {
threads[i] = new Thread(new SynchronizedCounter(), "Thread-" + i);
threads[i].start();
}
for(int i = 0; i < threads.length; i++) {
try {
threads[i].join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
现在当我执行时,得到了这个结果:
Thread-0 :: reads --> 0
Thread-0 :: updated value --> 1
Thread-3 :: reads --> 1
Thread-3 :: updated value --> 2
Thread-0 :: reads --> 2
Thread-0 :: updated value --> 3
Thread-0 :: reads --> 3
Thread-0 :: updated value --> 4
Thread-0 :: reads --> 4
Thread-0 :: updated value --> 5
Thread-0 :: reads --> 5
Thread-0 :: updated value --> 6
Thread-0 :: reads --> 6
Thread-0 :: updated value --> 7
Thread-0 :: reads --> 7
Thread-0 :: updated value --> 8
Thread-0 :: reads --> 8
Thread-0 :: updated value --> 9
Thread-0 :: reads --> 9
Thread-0 :: updated value --> 10
Thread-1 :: reads --> 10
Thread-1 :: updated value --> 11
Thread-2 :: reads --> 11
Thread-2 :: updated value --> 12
Thread-4 :: reads --> 12
Thread-4 :: updated value --> 13
Thread-3 :: reads --> 13
Thread-3 :: updated value --> 14
一切看起来都不错,但我不明白为什么 "counter" 的值会超过 10。即使值是 13 或 14,也就是大于 10,while 循环是如何通过的。我知道当控件试图当它可能读取不同的值时输入,并且当它第一次到达时打印它,即“:: reads -->”。但理想情况下,任何线程都应将此值增加到 10。请帮助我了解我遗漏的地方。
谢谢!!
因为synchronized(SynchronizedCounter.class)在while循环中。想象计数器命中 9。所有线程仍然可以进入 while 块,然后一个线程将 运行,将计数器递增到 10。它将退出同步块,允许另一个线程 运行,将计数器递增到11. 每个线程都如此,因此计数器将达到 14。然后下一次通过 while 循环时,计数器 < 10 测试将失败,它们将从 运行( ), 从而退出。
我猜同步块中的另一个计数器 < 10 测试会修复它。
我在刷我的多线程基础。为了理解同步,我创建了以下程序-
package thread;
public class SynchronizedCounter implements Runnable{
private static int counter = 0;
public void run() {
while(counter < 10) {
synchronized (SynchronizedCounter.class) {
System.out.println(Thread.currentThread().getName() + " :: reads ## " + counter);
counter++;
System.out.println(Thread.currentThread().getName() + " :: updated value ## " + counter);
}
}
}
public static void main(String[] args) {
Thread[] threads = new Thread[5];
for(int i = 0; i < threads.length; i++) {
threads[i] = new Thread(new SynchronizedCounter(), "Thread-" + i);
threads[i].start();
}
for(int i = 0; i < threads.length; i++) {
try {
threads[i].join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
现在当我执行时,得到了这个结果:
Thread-0 :: reads --> 0
Thread-0 :: updated value --> 1
Thread-3 :: reads --> 1
Thread-3 :: updated value --> 2
Thread-0 :: reads --> 2
Thread-0 :: updated value --> 3
Thread-0 :: reads --> 3
Thread-0 :: updated value --> 4
Thread-0 :: reads --> 4
Thread-0 :: updated value --> 5
Thread-0 :: reads --> 5
Thread-0 :: updated value --> 6
Thread-0 :: reads --> 6
Thread-0 :: updated value --> 7
Thread-0 :: reads --> 7
Thread-0 :: updated value --> 8
Thread-0 :: reads --> 8
Thread-0 :: updated value --> 9
Thread-0 :: reads --> 9
Thread-0 :: updated value --> 10
Thread-1 :: reads --> 10
Thread-1 :: updated value --> 11
Thread-2 :: reads --> 11
Thread-2 :: updated value --> 12
Thread-4 :: reads --> 12
Thread-4 :: updated value --> 13
Thread-3 :: reads --> 13
Thread-3 :: updated value --> 14
一切看起来都不错,但我不明白为什么 "counter" 的值会超过 10。即使值是 13 或 14,也就是大于 10,while 循环是如何通过的。我知道当控件试图当它可能读取不同的值时输入,并且当它第一次到达时打印它,即“:: reads -->”。但理想情况下,任何线程都应将此值增加到 10。请帮助我了解我遗漏的地方。
谢谢!!
因为synchronized(SynchronizedCounter.class)在while循环中。想象计数器命中 9。所有线程仍然可以进入 while 块,然后一个线程将 运行,将计数器递增到 10。它将退出同步块,允许另一个线程 运行,将计数器递增到11. 每个线程都如此,因此计数器将达到 14。然后下一次通过 while 循环时,计数器 < 10 测试将失败,它们将从 运行( ), 从而退出。
我猜同步块中的另一个计数器 < 10 测试会修复它。