多線程發送死鎖一般情況就是同步中嵌套同步。
其實就是
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(); } }