Monday, 11 December 2017

How To Detect The Deadlocked Threads In Java

Deadlock in java, How To Detect The Deadlocked Threads In Java,How To Detect The Deadlocked ,How To Detect The Deadlocked Threads,ThreadMXBean () method,getThreadMXBean(),findMonitorDeadlockedThreads(),getThreadInfo(),


in this tutorial we will learn about how to detect the Deadlock in java. we know the what is deadlock?, but you know weather it can be detected or not. let see it...
This can be done using ThreadMXBean interface of java.lang.Management package. You can go through the oracle docs of ThreadMXBean interface.
First, you have to get an instance of ThreadMXBean using getThreadMXBean() method of ManagementFactory, like this.

ThreadMXBean bean = ManagementFactory.getThreadMXBean();
long ids[] = bean.findMonitorDeadlockedThreads();
ThreadInfo threadInfo[] = bean.getThreadInfo(ids);



for (ThreadInfo threadInfo1 : threadInfo)
{
   System.out.println(threadInfo1.getThreadName());    //Prints the name of deadlocked thread
}

After getting an instance of ThreadMXBean, call findMonitorDeadlockedThreads() method on it. It returns an array of type long containing ids of all currently deadlocked threads.

After getting the ids of deadlocked threads, pass these ids to getThreadInfo() method of ThreadMXBean. It will return an array of ThreadInfo objects, where one ThreadInfo object contains the details of one deadlocked thread.
Iterate the ThreadInfo array to get the details of individual deadlocked thread.


Let see a complete example of deadlock detection and get the details of deadlock thread.

import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;

class Shared
{
    synchronized void methodOne(Shared s)
    {
        Thread t = Thread.currentThread();

        System.out.println(t.getName()+"is executing methodOne...");

        try
        {
            Thread.sleep(2000);
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }

        System.out.println(t.getName()+"is calling methodTwo...");

        s.methodTwo(this);

        System.out.println(t.getName()+"is finished executing methodOne...");
    }

    synchronized void methodTwo(Shared s)
    {
        Thread t = Thread.currentThread();

        System.out.println(t.getName()+"is executing methodTwo...");

        try
        {
            Thread.sleep(2000);
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }

        System.out.println(t.getName()+"is calling methodOne...");

        s.methodOne(this);

        System.out.println(t.getName()+"is finished executing methodTwo...");
    }
}

public class ThreadsInJava
{
    public static void main(String[] args)
    {
        final Shared s1 = new Shared();

        final Shared s2 = new Shared();

        Thread t1 = new Thread()
        {
            public void run()
            {
                s1.methodOne(s2);
            }
        };

        Thread t2 = new Thread()
        {
            @Override
            public void run()
            {
                s2.methodTwo(s1);
            }
        };

        t1.start();

        t2.start();

        try
        {
            Thread.sleep(5000);
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }

        ThreadMXBean bean = ManagementFactory.getThreadMXBean();

        long ids[] = bean.findMonitorDeadlockedThreads();

        if(ids != null)
        {
            ThreadInfo threadInfo[] = bean.getThreadInfo(ids);

            for (ThreadInfo threadInfo1 : threadInfo)
            {
                System.out.println(threadInfo1.getThreadId());    //Prints the ID of deadlocked thread

                System.out.println(threadInfo1.getThreadName());  //Prints the  name of deadlocked thread

                System.out.println(threadInfo1.getLockName());    //Prints the string representation of an object for which thread has entered into deadlock.

                System.out.println(threadInfo1.getLockOwnerId());  //Prints the ID of thread which currently owns the object lock

                System.out.println(threadInfo1.getLockOwnerName());  //Prints name of the thread which currently owns the object lock.
            }
        }
        else
        {
            System.out.println("No Deadlocked Threads");
        }
    }
}

DashZin

Author & Editor

Has laoreet percipitur ad. Vide interesset in mei, no his legimus verterem. Et nostrum imperdiet appellantur usu, mnesarchum referrentur id vim.

10 comments:

  1. D074E0A7E4OscarB9B1DC5CA812 October 2024 at 09:51

    21D48DCAB9
    skype şov sitesi

    ReplyDelete