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");
}
}
}
elazığ
ReplyDeletebilecik
kilis
sakarya
yozgat
YEBE
kayseri evden eve nakliyat
ReplyDeleteantalya evden eve nakliyat
izmir evden eve nakliyat
nevşehir evden eve nakliyat
kayseri evden eve nakliyat
VT6Y
D13EF
ReplyDeleteEryaman Alkollü Mekanlar
Bingöl Parça Eşya Taşıma
Tunceli Evden Eve Nakliyat
Erzurum Evden Eve Nakliyat
Bitlis Lojistik
Isparta Evden Eve Nakliyat
Lbank Güvenilir mi
Ünye Çatı Ustası
Çerkezköy Korkuluk
44F54
ReplyDeleteBalıkesir Şehirler Arası Nakliyat
Çerkezköy Çatı Ustası
Kilis Şehirler Arası Nakliyat
Eskişehir Şehir İçi Nakliyat
Bursa Şehirler Arası Nakliyat
Çerkezköy Oto Lastik
Etimesgut Boya Ustası
Bingöl Parça Eşya Taşıma
Tokat Parça Eşya Taşıma
9831D
ReplyDeleteBinance Referans Kodu
Burdur Şehirler Arası Nakliyat
Kripto Para Borsaları
Bartın Evden Eve Nakliyat
Giresun Lojistik
Zonguldak Şehirler Arası Nakliyat
Malatya Parça Eşya Taşıma
Edirne Lojistik
Ünye Halı Yıkama
C8F7F
ReplyDeletereferans kodu
854FC
ReplyDeletehttps://referanskodunedir.com.tr/
255A5
ReplyDeletetrabzon kadınlarla sohbet
Ardahan Rastgele Görüntülü Sohbet Uygulamaları
van canlı ücretsiz sohbet
nevşehir rastgele sohbet siteleri
nanytoo sohbet
çankırı ücretsiz görüntülü sohbet uygulamaları
kastamonu sohbet muhabbet
burdur bedava görüntülü sohbet sitesi
sohbet sitesi
94933
ReplyDeleteyearn finance
eigenlayer
sushiswap
DefiLlama
pancakeswap
uniswap
pudgy penguins
shapeshift
layerzero
21D48DCAB9
ReplyDeleteskype şov sitesi