Java同步似乎被忽略了

发布于 2024-08-26 18:22:10 字数 1357 浏览 5 评论 0原文

我有以下代码,我预计在打印出“Main:预同步”后会死锁。但看起来 synchronized 并没有达到我的预期。这里会发生什么?

import java.util.*;

public class deadtest {
   public static class waiter implements Runnable {
      Object obj;

      public waiter(Object obj) {
         this.obj = obj;
      }

      public void run() {
         System.err.println("Thead: pre-sync");
         synchronized(obj) {
            System.err.println("Thead: pre-wait");
            try {
               obj.wait();
            } catch (Exception e) {
            }
            System.err.println("Thead: post-wait");
         }
         System.err.println("Thead: post-sync");
      }
   }

   public static void main(String args[]) {
      Object obj = new Object();

      System.err.println("Main: pre-spawn");

      Thread waiterThread = new Thread(new waiter(obj));
      waiterThread.start();

      try {
         Thread.sleep(1000);
      } catch (Exception e) {
      }  

      System.err.println("Main: pre-sync");
      synchronized(obj) {
         System.err.println("Main: pre-notify");
         obj.notify();
         System.err.println("Main: post-notify");
      }
      System.err.println("Main: post-sync");

      try {
         waiterThread.join();
      } catch (Exception e) {
      }
   }
}

由于两个线程在创建的对象上同步,因此我预计线程实际上会相互阻塞。目前,代码愉快地通知其他线程、加入和退出。

I've got the following code, which I expected to deadlock after printing out "Main: pre-sync". But it looks like synchronized doesn't do what I expect it to. What happens here?

import java.util.*;

public class deadtest {
   public static class waiter implements Runnable {
      Object obj;

      public waiter(Object obj) {
         this.obj = obj;
      }

      public void run() {
         System.err.println("Thead: pre-sync");
         synchronized(obj) {
            System.err.println("Thead: pre-wait");
            try {
               obj.wait();
            } catch (Exception e) {
            }
            System.err.println("Thead: post-wait");
         }
         System.err.println("Thead: post-sync");
      }
   }

   public static void main(String args[]) {
      Object obj = new Object();

      System.err.println("Main: pre-spawn");

      Thread waiterThread = new Thread(new waiter(obj));
      waiterThread.start();

      try {
         Thread.sleep(1000);
      } catch (Exception e) {
      }  

      System.err.println("Main: pre-sync");
      synchronized(obj) {
         System.err.println("Main: pre-notify");
         obj.notify();
         System.err.println("Main: post-notify");
      }
      System.err.println("Main: post-sync");

      try {
         waiterThread.join();
      } catch (Exception e) {
      }
   }
}

Since both threads synchronize on the created object, I expected the threads to actually block each other. Currently, the code happily notifies the other thread, joins and exits.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

苏辞 2024-09-02 18:22:10

在监视器上调用 .wait() 实际上会释放同步锁,以便其他线程可以锁定同一监视器并发送通知。

您的行为完全正常:“服务员”锁定监视器,然后在等待通知时释放锁定。 1秒后,主线程锁定监视器,发送通知,解锁监视器,从而唤醒等待者以完成其操作。

Calling .wait() on a monitor actually releases the synchronized lock so the other thread can lock on to the same monitor and send a notification.

Your behavior is completly normal: "waiter" locks on a monitor and then releases the lock when waiting for notification. After 1 second the main thread locks the monitor, sends notification, unlocks the monitor, which wakes the waiter to complete its operation.

病毒体 2024-09-02 18:22:10

当您对某个对象wait() 时,该线程会释放该对象上的锁,以允许其他线程获取该锁并notify() 等待线程。请参阅 javadoc 了解 Object.wait()

当前线程必须拥有该对象的监视器。 线程释放此监视器的所有权并等待,直到另一个线程通过调用notify方法或notifyAll方法通知在此对象监视器上等待的线程唤醒。 然后线程等待,直到它可以重新获得监视器的所有权并恢复执行

When you wait() on an object, the thread releases the lock on the object to allow others to aquire the lock and notify() the waiting thread. See the javadoc for Object.wait().

The current thread must own this object's monitor. The thread releases ownership of this monitor and waits until another thread notifies threads waiting on this object's monitor to wake up either through a call to the notify method or the notifyAll method. The thread then waits until it can re-obtain ownership of the monitor and resumes execution.

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