title: What happens when two locked threads (by a variable), switch one of their variable and try to access another?
body: <p>(I just started threads, so there is a big chance I'll be wrong) </p>
  
  <p>If two threads of the same class, one of their variables gets into a synchronized method, and they switch one of their variables, and try to access another synchronized method using a switched variable. a deadlock will occur? and why?</p>
  
  <p>An example of what I'm talking about: The class:</p>
  
  <pre><code>public class Person extends Thread{
      public Hand leftHand;
      public Hand rightHand;
      public String name;
      public Person friend;
  
      public Person(Hand a, Hand b, String name, Person f){
          this.leftHand = a;
          this.rightHand = b;
          this.name = name;
          this.friend = f;
      }
  
      public void switchHands(){
          if(leftHand.color.equals(rightHand.color)){
              Hand temp = this.rightHand;
              this.rightHand = friend.rightHand;
              friend.rightHand = temp;
          }
      }
  
      public void run(){
          synchronized (leftHand){
              System.out.println(this.name + " locked with " + this.leftHand);
              switchHands();
              synchronized (rightHand){
                   System.out.println(this.name + " locked with " + this.rightHand);   
              }
          }
      }
  
      public static class Hand{
          String color;
          public Hand(String c){ this.color = c; }  
      }
  }
  </code></pre>
  
  <p>And in the main:</p>
  
  <pre><code>public static void main(String[] args){
      Hand whiteHand = new Hand("white");
      Hand blackHand = new Hand("black");
      Person one = new Person(whiteHand, whiteHand, "one", null);
      Person second = new Person(blackHand, blackHand, "second", null);
      one.friend = second;
      second.friend = one;
      one.start();
      second.start()
  }
  </code></pre>
  
  <p>as you can see, the two threads (one and two), gets locked in <code>synchronized (leftHand)</code> , afterwards one of the threads at least switch hands (the right hand), and then - we try to access <code>synchronized (rightHand)</code>, and a deadlock probably occurred.</p>
  
  <p>I can understand the logic, but when I switch the hands, intuitively I think I'll just copy the content of the other hand, but <strong>I don't understand why a deadlock occurs</strong>. </p>
  
  <p>P.S I think my title is not precise enough, so an edit will be welcome.</p>