package multithread;
class Mylock
{
public static Object o1 = new Object();
public static Object o2 = new Object();
}
class demo implements Runnable
{
private boolean flag;
demo(boolean flag)
{
this.flag =flag;
}
public void run()
{
if(flag)
{
synchronized(Mylock.o1)
{
System.out.println(Thread.currentThread().getName()+"........if o1............");
synchronized(Mylock.o2)
{
System.out.println(Thread.currentThread().getName()+"........if o2............");
}
}
}
else
{
synchronized(Mylock.o2)
{
System.out.println(Thread.currentThread().getName()+"........if o2............");
synchronized(Mylock.o1)
{
System.out.println(Thread.currentThread().getName()+"........if o1............");
}
}
}
}
}
class Deadlock
{
public static void main(String[] args) {
demo d= new demo(true);
demo d2 = new demo(false);
Thread t1 = new Thread(d);
Thread t2 = new Thread(d2);
t1.start();
t2.start();
}
}
死锁例子
最新推荐文章于 2022-11-29 10:53:14 发布