title: How synchronization of threads work along with Sleep()?
body: <pre><code>class ABC extends Thread
  
  {
  
      public void run()
      {
          display();
      }
  
      public synchronized void display()
      {
          {
              System.out.println("1");            
              try
              {
                  this.sleep(1000);
  
              }catch(Exception e)
              {
  
              }
              System.out.println("2");
          }
      }
  }
  
  public class Thread_10 
  
  {
  
      public static void main(String[] args)  
      {
          ABC t1=new ABC();
          t1.start();
          ABC t2=new ABC();
          t2.start();
      }
  }
  </code></pre>
  
  <p>OUTPUT :- 
  1
  1
  2
  2</p>
  
  <p>I wanted to understand the concept of synchronization. As you can see, I have a sleep() inside  display(). Now, when thread "t1" executes run() and goes to sleep after printing "1", why does thread "t2" enters display() through run()? because, my function display() is declared synchronized, so it is already locked by thread "t1". 
  I was expecting the following output:-</p>
  
  <p>1
  2
  1
  2</p>