多线程死锁
package test.thread.dielock; /* * 线程死锁:锁嵌套锁 * 在开如应避免锁嵌套锁 */ public class DeadLock { private static String knife="刀"; private static String fork="叉"; public static void main(String[] args) { new Thread(new Runnable() { public void run() { //锁嵌套锁 while(true) { synchronized (knife) {//线程1先拿刀 System.out.println(Thread.currentThread().getName()+"已经拿到"+knife+"等待"+fork); synchronized (fork) {//线程1再拿叉 System.out.println(Thread.currentThread().getName()+"拿到"+fork+"开吃!"); } } } } },"one").start(); new Thread(new Runnable() { public void run() { //锁嵌套锁 while(true) { synchronized (fork) {//线程2先拿叉子 System.out.println(Thread.currentThread().getName()+"已经拿到"+fork+"等待"+knife); synchronized (knife) {//线程2再拿刀 System.out.println(Thread.currentThread().getName()+"拿到"+knife+"开吃!"); } } } } },"tow").start(); } }