多个线程模拟人过山洞
package www.whf.test2;
public class test {
public static void main(String[] args) {
for(int i=0;i<20;i++){
new Thread(new Man("第 "+i+" "+Math.random())).start();
}
}
static class cave{
static boolean flag=true;
public static synchronized boolean isfree(){
return flag;
}
public static synchronized void busy(){
flag=false;
}
public static synchronized void free(){
flag=false;
}
public static synchronized void pass(Man man){
System.out.println(man.name+"进入");
try {
Thread.sleep(2000);
System.out.println(man.name+"通过");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// public synchronized void pass(man man){
// new Thread(man).start();
// }
}
}