类的静态方法之间的同步

发布于 2024-10-14 16:54:36 字数 1311 浏览 3 评论 0原文

我编写了以下代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一个人的旅程 2024-10-21 16:54:36

根据我的理解,当 new Thread(new EvenThread()).start();执行时,它会生成一个 EvenThread 类的线程,该线程应该获取 CommonClass 的锁并保持该锁,直到它打印所有偶数值。

不,事实并非如此:

 public static void printOddNumber(int num) {
   synchronized (CommonClass.class) {
       System.out.println("Odd :" + num);
   }
 }

哪个(因为该方法在监视器类上是静态的),也可以写为

 public static synchronized void printOddNumber(int num) {
       System.out.println("Odd :" + num);
 }

查看同步块的范围:它仅打印一个数字,然后释放锁。

如果您想在不被中断的情况下打印所有数字,则必须在整个 for 循环期间锁定:

public void run() {
  synchronized(CommonClass.class){
    for (int i = 0; i < 10; i += 2) {
     System.out.println("Even :" + num);
    }
  }
}

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.

No, that is not the case:

 public static void printOddNumber(int num) {
   synchronized (CommonClass.class) {
       System.out.println("Odd :" + num);
   }
 }

which (since the method is static on the monitor class), can also be written as

 public static synchronized void printOddNumber(int num) {
       System.out.println("Odd :" + num);
 }

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:

public void run() {
  synchronized(CommonClass.class){
    for (int i = 0; i < 10; i += 2) {
     System.out.println("Even :" + num);
    }
  }
}
小傻瓜 2024-10-21 16:54:36

您的同步是在打印方法级别,这意味着每次打印都会获取和释放锁。如果您希望线程在持续时间内保持锁定,则必须在线程的 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文