多线程发送死锁一般情况就是同步中嵌套同步。
其实就是
snychronized(锁1)
{
snychronized(锁2)
}
而发送死锁
/*
多线程的死锁
同步中嵌套同步;
主要是避免写程序发生死锁现象。
*/
class Test11 implements Runnable
{
private boolean flag;
Test11(boolean flag)
{
this.flag = flag;
}
public void run()
{
if(flag)
{
while(true)
{
synchronized(MyLock.locka)
{
System.out.println("if locka");
synchronized(MyLock.lockb)
{
System.out.println("if lockb");
}
}
}
}
else
{
synchronized(MyLock.lockb)
{
System.out.println("else lockb");
synchronized(MyLock.locka)
{
System.out.println("else locka");
}
}
}
}
}
class MyLock
{
static Object locka = new Object();
static Object lockb = new Object();
}
class Test_11_15
{
public static void main(String[] args)
{
System.out.println("hello wolrd");
Thread t1 = new Thread(new Test11(true));
Thread t2 = new Thread(new Test11(false));
t1.start();
t2.start();
}
}
阅读(2238) | 评论(0) | 转发(0) |