Thursday, 9 February 2017

Java Tutorial: Java deadlock | deadlock in java


Click here to watch in Youtube :
https://www.youtube.com/watch?v=SRW7Q1gARPc&list=UUhwKlOVR041tngjerWxVccw

Click the below Image to Enlarge
Java Tutorial: Java deadlock | deadlock in java 
Java Tutorial: Java deadlock | deadlock in java 
Java Tutorial: Java deadlock | deadlock in java 
Java Tutorial: Java deadlock | deadlock in java 
Java Tutorial: Java deadlock | deadlock in java 
Java Tutorial: Java deadlock | deadlock in java 
Java Tutorial: Java deadlock | deadlock in java 
Java Tutorial: Java deadlock | deadlock in java 
Java Tutorial: Java deadlock | deadlock in java 
Deadlock.java
public class Deadlock
{
    public static void main(String[] args)
    {
        final String resource1 = "Hello";
        final String resource2 = "Peter";

        /*
         * t1 tries to lock resource1 then resource2
         */
        Thread t1 = new Thread()
        {
            public void run()
            {
                synchronized (resource1)
                {
                    System.out.println("Thread 1: locked resource 1");

                    try
                    {
                        Thread.sleep(100);
                    }
                    catch (Exception e)
                    {
                    }

                    synchronized (resource2)
                    {
                        System.out.println("Thread 1: locked resource 2");
                    }
                }
            }
        };

        /*
         * t2 tries to lock resource2 then resource1
         */
        Thread t2 = new Thread()
        {
            public void run()
            {
                synchronized (resource2)
                {
                    System.out.println("Thread 2: locked resource 2");

                    try
                    {
                        Thread.sleep(100);
                    }
                    catch (Exception e)
                    {
                    }

                    synchronized (resource1)
                    {
                        System.out.println("Thread 2: locked resource 1");
                    }
                }
            }
        };

        t1.start();
        t2.start();
    }
}
Output
Thread 1: locked resource 1
Thread 2: locked resource 2

Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/Thread_DeadLockDemo_App.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/Thread_DeadLockDemo_App

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/644abf74fceeb240066dd3d2fe2e4b1fb6c4bd48/BasicJava/Thread_DeadLockDemo_App/?at=master

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • No comments:

    Post a Comment