类的静态方法之间的同步
我编写了以下代码:
public class ClassAndObjectLock {
public static void main(String[] args) {
new Thread(new EvenThread()).start();
new Thread(new OddThread()).start();
}
}
class EvenThread implements Runnable {
public void run() {
for (int i = 0; i < 10; i += 2) {
CommonClass.printEvenNumber(i);
}
}
}
class OddThread implements Runnable {
public void run() {
for (int i = 1; i < 10; i += 2) {
CommonClass.printOddNumber(i);
}
}
}
class CommonClass {
public static void printEvenNumber(int num) {
synchronized (CommonClass.class) {
System.out.println("Even :" + num);
}
}
public static void printOddNumber(int num) {
synchronized (CommonClass.class) {
System.out.println("Odd :" + num);
}
}
}
当我执行上述代码时,输出如下:
Even :0
Odd :1
Even :2
Odd :3
Even :4
Odd :5
Even :6
Odd :7
Even :8
Odd :9
我不明白这个输出。根据我的理解,当执行 new Thread(new EvenThread()).start();
时,它会生成一个 EvenThread
类的线程,该线程应该获取 EvenThread
的锁code>CommonClass 并保持自身锁定,直到打印出所有偶数值。只有在 EvenThread
对象完成后,OddThread
对象才应该获得机会。因此,在我看来,输出应该如下:
Even :0
Even :2
Even :4
Even :6
Even :8
Odd :1
Odd :3
Odd :5
Odd :7
Odd :9
谁能解释一下底层逻辑吗?
I've written the following code:
public class ClassAndObjectLock {
public static void main(String[] args) {
new Thread(new EvenThread()).start();
new Thread(new OddThread()).start();
}
}
class EvenThread implements Runnable {
public void run() {
for (int i = 0; i < 10; i += 2) {
CommonClass.printEvenNumber(i);
}
}
}
class OddThread implements Runnable {
public void run() {
for (int i = 1; i < 10; i += 2) {
CommonClass.printOddNumber(i);
}
}
}
class CommonClass {
public static void printEvenNumber(int num) {
synchronized (CommonClass.class) {
System.out.println("Even :" + num);
}
}
public static void printOddNumber(int num) {
synchronized (CommonClass.class) {
System.out.println("Odd :" + num);
}
}
}
When I execute the above code, following is the output:
Even :0
Odd :1
Even :2
Odd :3
Even :4
Odd :5
Even :6
Odd :7
Even :8
Odd :9
I do not understand this output. As per my understanding, when new Thread(new EvenThread()).start();
is executed, it spawns a thread of class EvenThread
which should acquire the lock of CommonClass
and keep the lock with itself until it has printed all the even values. The object of OddThread
should get a chance only after the object of EvenThread
has finished. Therefore in my opinion the output should be the following:
Even :0
Even :2
Even :4
Even :6
Even :8
Odd :1
Odd :3
Odd :5
Odd :7
Odd :9
Could anyone please explain me the underlying logic?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,事实并非如此:
哪个(因为该方法在监视器类上是静态的),也可以写为
查看同步块的范围:它仅打印一个数字,然后释放锁。
如果您想在不被中断的情况下打印所有数字,则必须在整个 for 循环期间锁定:
No, that is not the case:
which (since the method is static on the monitor class), can also be written as
Look at the scope of the synchronized block: It prints just one number and then releases the lock.
If you want to print all numbers without being interrupted, you have to lock for the duration of your whole for loop:
您的同步是在打印方法级别,这意味着每次打印都会获取和释放锁。如果您希望线程在持续时间内保持锁定,则必须在线程的 run 方法中同步该对象(在循环周围)。
Your synchronization is at the level of print methods, which means that every print acquires and releases the lock. If you want the thread to hold the lock for the duration, you have to synchronize on that object in the thread's run method instead (around the loop).