5 Replies Latest reply: Nov 24, 2009 5:20 AM by 843789 RSS

    Deadlock + syncronization

    843789
      hi,

      i jhave been reading on sun site on threads i have certains doubts
      according my understanding synchronized methods allow one thread at a time to excute if

      then the programme below should have been safe but y does it get in deadlock sorry if i am nut

      public class Deadlock {
      static class Friend {
      private final String name;
      public Friend(String name) {
      this.name = name;
      }
      public String getName() {
      return this.name;
      }
      public synchronized void bow(Friend bower) { 
      System.out.format("%s: %s has bowed to me!%n",
      this.name, bower.getName());
      bower.bowBack(this);
      }
      public synchronized void bowBack(Friend bower) {
      System.out.format("%s: %s has bowed back to me!%n",
      this.name, bower.getName());
      }
      }

      public static void main(String[] args) {
      final Friend alphonse = new Friend("Alphonse");
      final Friend gaston = new Friend("Gaston");
      new Thread(new Runnable() {
      public void run() { alphonse.bow(gaston); }
      }).start();
      new Thread(new Runnable() {
      public void run() { gaston.bow(alphonse); }
      }).start();
      }
      }


      please explain
        • 1. Re: Deadlock + syncronization
          843789
          Hi

          I have run your code and it works (no deadlock)...

          The ouput is like expected:

          Alphonse: Gaston has bowed to me!
          Gaston: Alphonse has bowed back to me!
          Gaston: Alphonse has bowed to me!
          Alphonse: Gaston has bowed back to me!

          Why do you belive that you have a deadlock?
          • 2. Re: Deadlock + syncronization
            843789
            Ups... I'm wrong now i got the deadlock....

            Alphonse: Gaston has bowed to me!
            Gaston: Alphonse has bowed to me!
            • 3. Re: Deadlock + syncronization
              jduprez
              Hello,

              Please use { code } tags when posting code, or select the code in the post editor and use the CODE button.
              according my understanding synchronized methods allow one thread at a time to excute
              It's a bit more complex than that.
              First, the synchronization is per instance:
              That is, two threads can be executing the same method of two different instances: such as, two threads may be executing alphonse.bow() and gaston.bow() concurrently).

              Second, synchronization is per instance :o)
              That is, two threads cannot be executing two different synchronized methods on the same instance: such as, if thread T1 has started executing alphonse.bow(), thread T2 cannot execute alphonse.bowBack() (which is indeed called from gaston.bow(alphonse)) until T1 has exited method bow().

              Hope this helps.
              • 4. Re: Deadlock + syncronization
                843789
                [http://java.sun.com/docs/books/tutorial/essential/concurrency/syncmeth.html]

                From the above link...

                First, it is not possible for two invocations of synchronized methods on the same object to interleave. When one thread is executing a synchronized method for an object, all other threads that invoke synchronized methods for the same object block (suspend execution) until the first thread is done with the object.
                • 5. Re: Deadlock + syncronization
                  YoungWinston
                  montsie wrote:
                  Ups... I'm wrong now i got the deadlock....
                  One of the natures of deadlocks (at least as I understand it :-)): they don't always occur, and are often difficult to recreate.

                  Chapter 10 of Effective Java (Bloch, 2nd edition) provides some good tips about deadlocks and how to avoid them, but there may be more comprehensive tracts out there.

                  Winston