Ads

Saturday, 18 November 2017

Synchronization In Java


Synchronization 


The Synchronization  ensures that only one thread will access a shared resources at a time. When a thread enters shared resources it will lock that resources and after the completion of its execution it resources will be unlocked.


The synchronization is done by the synchronized keyword.


Example of deadlock with synchronization 


class Th
{  
      public void put()
      { 
             System.out.println("Hello, This is The JavaXpert");
       }
}
class Thr extends Thread
{
       T obj;
        public Thr()
        {
                           System.out.println("Constructor call...");
        }
        public void run()  
        {
                         obj.put();
        }
}
class Deadlockdemo
{
         public static void main(String args[])  
         {
                  Thr t1=new Thr(); 
                  Thr t2=new Thr(); 
                  t1.start();
                  t2.start();
          }
}
                  
 
Post a Comment

Click Here