for循环中的同步方法
synchronized method in for loop
在下面的简单示例代码中(主要取自此nice udemy video),有两个线程通过synchronized
方法递增计数实例变量。
然而,此方法在每个线程的 for
循环中。
我的问题是:在这种情况下,线程是否可能实际上混合了对 increment
的调用?例如:
Thread 1 calls increment for i = 0
Thread 1 calls increment for i = 1
Thread 2 calls increment for j = 0
Thread 2 calls increment for j = 1
Thread 1 calls increment for i = 2
Thread 2 calls increment for j = 2
... and so on
我为大 i
和 j
尝试了很多次 运行,但它们总是按顺序显示,即
Thread 1 calls increment for i = 0
Thread 1 calls increment for i = 1
Thread 1 calls increment for i = 2
... Thread 1 finishes
Thread 2 calls increment for j = 0
Thread 2 calls increment for j = 1
... Thread 2 finishes
根据我对 synchronized
关键字的理解,第一种情况应该会发生,所以我只是想知道我是否看到第二种情况,因为我有可能遗漏了其他东西。
public class SyncTest {
private int count = 0;
public static void main(String[] args) {
SyncTest test = new SyncTest ();
test.doWork();
}
public synchronized void increment(String threadName) {
System.out.println("thread: " + threadName);
count++;
}
public void doWork() {
Thread t1 = new Thread(new Runnable() {
public void run() {
for (int i = 0; i < 100000; i++) {
increment("t1");
}
}
});
Thread t2 = new Thread(new Runnable() {
public void run() {
for (int j = 0; j < 100000; j++) {
increment("t2");
}
}
});
t1.start();
t2.start();
try {
t1.join();
t2.join();
} catch(InterruptedException e) {
e.printStackTrace();
}
System.out.println("Count is: " + count);
}
}
Based on my understanding on the synchronized keyword, the first case should happen, so I am just wondering if I am seeing the second case due to chance of there is something else that I am missing.
你的理解是正确的。
事实上,当我 运行 你的确切代码时,我得到了一个 t1
序列,然后是一个 t2
序列,然后又是 t1
序列, 然后是 t2
s 等等。这清楚地表明线程正在相互抢占,并且没有我们忽略的微妙的意外同步。
在下面的简单示例代码中(主要取自此nice udemy video),有两个线程通过synchronized
方法递增计数实例变量。
然而,此方法在每个线程的 for
循环中。
我的问题是:在这种情况下,线程是否可能实际上混合了对 increment
的调用?例如:
Thread 1 calls increment for i = 0
Thread 1 calls increment for i = 1
Thread 2 calls increment for j = 0
Thread 2 calls increment for j = 1
Thread 1 calls increment for i = 2
Thread 2 calls increment for j = 2
... and so on
我为大 i
和 j
尝试了很多次 运行,但它们总是按顺序显示,即
Thread 1 calls increment for i = 0
Thread 1 calls increment for i = 1
Thread 1 calls increment for i = 2
... Thread 1 finishes
Thread 2 calls increment for j = 0
Thread 2 calls increment for j = 1
... Thread 2 finishes
根据我对 synchronized
关键字的理解,第一种情况应该会发生,所以我只是想知道我是否看到第二种情况,因为我有可能遗漏了其他东西。
public class SyncTest {
private int count = 0;
public static void main(String[] args) {
SyncTest test = new SyncTest ();
test.doWork();
}
public synchronized void increment(String threadName) {
System.out.println("thread: " + threadName);
count++;
}
public void doWork() {
Thread t1 = new Thread(new Runnable() {
public void run() {
for (int i = 0; i < 100000; i++) {
increment("t1");
}
}
});
Thread t2 = new Thread(new Runnable() {
public void run() {
for (int j = 0; j < 100000; j++) {
increment("t2");
}
}
});
t1.start();
t2.start();
try {
t1.join();
t2.join();
} catch(InterruptedException e) {
e.printStackTrace();
}
System.out.println("Count is: " + count);
}
}
Based on my understanding on the synchronized keyword, the first case should happen, so I am just wondering if I am seeing the second case due to chance of there is something else that I am missing.
你的理解是正确的。
事实上,当我 运行 你的确切代码时,我得到了一个 t1
序列,然后是一个 t2
序列,然后又是 t1
序列, 然后是 t2
s 等等。这清楚地表明线程正在相互抢占,并且没有我们忽略的微妙的意外同步。