| 本帖最后由 moonlight 于 2011-11-20 17:20 编辑 
 public class Deadlock {
 public static void main(String[] args) {
 // TODO Auto-generated method stub
 Dead d1=new Dead();
 Thread t1=new Thread(d1);
 Thread t2=new Thread(d1);
 t1.setName("d1");
 t2.setName("d2");
 t1.start();
 t2.start();
 }
 }
 
 class Dead implements Runnable{
 int flag;
 public void run(){
 if(Thread.currentThread().getName().equals("d1")){
 m();
 }
 if(Thread.currentThread().getName().equals("d2")){
 n();
 }
 }
 
 public  synchronized void m(){
 System.out.println(Thread.currentThread().getName()+"正在用呢m()");
 try {
 Thread.sleep(1000);
 } catch (InterruptedException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }
 n();
 }
 
 public  synchronized void n(){
 System.out.println(Thread.currentThread().getName()+"正在用呢n()");
 try {
 Thread.sleep(2000);
 } catch (InterruptedException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }
 m();
 }
 }
 |