Java DeadLock:

Deadlock is one of the very important point in multithreading. Deadlock is a situation where a thread is waiting for an object lock, that is acquired by another thread and second thread is waiting for an object lock that is acquired by first thread. Since, both threads are waiting for each other to release the lock, as a result no thread can execute. This is known as deadlock. Lte's see a demo program on deadlock for better understanding:

DeadlockExample.java

class DeadlockExample

{

          public static void main(String[] args)

          {

                   String r1="Silan";

                   String r2="Technology";

                   //t1 trying to lock r1 and after 1000ms trying to lock r2

                   Thread t1=new Thread()

                   {

                             public void run()

                             {

                                       synchronized(r1)

                                      {

                                                System.out.println("In Thread t1:r1 locked");

                                                try

                                                {

                                                          Thread.sleep(1000);

                                                }

                                                catch(Exception e)

                                                {

                                                }

                                      synchronized(r2)

                                      {

                                                System.out.println("In Thread t1:r2 locked");

                                      }

                                      }

                             }

                   };

                     //t2 trying to lock r2 and after 1000ms trying to lock r1

                   Thread t2=new Thread()

                   {

                             public void run()

                             {

                                      synchronized(r2)

                                      {

                                                System.out.println("In Thread t2:r2 locked");

                                                try

                                                {

                                                          Thread.sleep(1000);

                                                }

                                                catch(Exception e)

                                                {

                                                }

                                      synchronized(r1)

                                      {

                                                System.out.println("In Thread t2:r1 locked");

                                      }

                                      }

                             }

                   };

                   t1.start();

                   t2.start();

          }

}

Output :

Our Services:

Java Training | Python Training | Machine Learning with Python Training | Deep Learning with Python Training | Data Science with Python Training | Projects

Silansoftware Pvt.Ltd, BBSR
Call to: 0674-2361252

Comment