日期:2014-05-20  浏览次数:20709 次

多线程死锁问题
Java code
class test implements Runnable{
    private boolean flag;
    public test(boolean flag){
        this.flag=flag;
    }
    public void run(){
        if(flag){
            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();
}
 public class DeadLockTest{
    public static void main(){
        Thread t1=new Thread(new test(true));
        Thread t2=new Thread(new test(false));
        t1.start();
        t2.start();
    }
}
想写个死锁,控制台输出一堆乱码 请问怎么回事

------解决方案--------------------
lz运行的是这个程序么?main方法参数捏?
------解决方案--------------------
大哥,你程序没错,但是你的main方法写错了

public static void main()
===》
public static void main(String[] args)

OK