4 Replies Latest reply: Jun 8, 2011 9:58 PM by 796440 RSS

    How to release the Lock???

    hkcoder
      Hi,

      I got the concept of the Deadlock from this code,but, i don't know how to release the deadlock in this type of condition. Can anybody please help me out???

      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();
      }
      }

      Many Thanks,
      - Hitesh
        • 1. Re: How to release the Lock???
          gimbal2
          Okay. What do you mean by "release the deadlock"? Deadlocks are something to fix permanently (IE. redesign your code so they don't happen), you don't try to deal with them in your code. At most I would put effort into being able to detect a deadlock if I suspect that one could happen; that is no easy thing to do however.
          • 2. Re: How to release the Lock???
            802316
            The proper fix is to make sure you never get a deadlock in the first place.

            If you have multiple locks you need to always lock them in the same order. If you are using "Lock" objects you can try to lock both locks and unlock them if you don't get both.

            A simpler approach is to use just one lock for both.
            e.g. if you make both methods static, you won't get a dead lock.
            • 3. Re: How to release the Lock???
              hkcoder
              Thanks jverd...I understood the concept...
              From now i'll take care of these things in case of Multithreaded communication...
              Many thanks to all for your kind support.
              - Hitesh
              • 4. Re: How to release the Lock???
                796440
                hkcoder wrote:
                Thanks jverd...I understood the concept...
                From now i'll take care of these things in case of Multithreaded communication...
                Many thanks to all for your kind support.
                - Hitesh
                My point was that you posted and then waited a whole 19 minutes before bumping your post. Although questions are often answered within a minute or two of being posted, you shouldn't expect it. People will answer when they get around to it. Bugging them after only a few minutes like you did is rude. Please remember that in the future.