代码问题多多,多线程需求也不明确。假设你的多线程同步条件是:同一时间只能有一个人在桥上。那么核心代码是
synchronized(lock){
    System.out.println(String.format("第%d个人从%s开始过桥", person[n-1].getNO(),person[n-1].getOrient()));
    Thread.sleep(2000);
}或者用Lock
lock.lock();
try{
    System.out.println(String.format("第%d个人从%s开始过桥", person[n-1].getNO(),person[n-1].getOrient()));
    Thread.sleep(2000);
}
finally{
    lock.unlock();
}按上面的同步条件,根本用不到 Condition 

解决方案 »

  1.   

    或者这样写while(!lock.tryLock()){
       Thread.sleep(1000);//如果获取不到锁,就休眠下
    }try{
        System.out.println(String.format("第%d个人从%s开始过桥", person[n-1].getNO(),person[n-1].getOrient())); 
        Thread.sleep(2000); 
    }
    finally{
        lock.unlock();
    }其实这样写,和直接lock.lock()功能是一样的,但是效率低一些.除非有特殊需求,比如尝试若干次还没得到锁定就直接退出