![](https://images2017.cnblogs.com/blog/725061/201712/725061-20171206175911847-1126073689.png)
public class test {
public static void main(String[] args) {
Tunnel tul = new Tunnel();
Thread p1 = new Thread(tul,"p1");
Thread p2 = new Thread(tul,"p2");
Thread p3 = new Thread(tul,"p3");
Thread p4 = new Thread(tul,"p4");
Thread p5 = new Thread(tul,"p5");
Thread p6 = new Thread(tul,"p6");
Thread p7 = new Thread(tul,"p7");
Thread p8 = new Thread(tul,"p8");
Thread p9 = new Thread(tul,"p9");
Thread p10 = new Thread(tul,"p10");
p1.start();
p2.start();
p3.start();
p4.start();
p5.start();
p6.start();
p7.start();
p8.start();
p9.start();
p10.start();
}
}
class Tunnel implements Runnable{
private int crossedNum = 0;
//override
public void run(){
Cross();
}
public synchronized void Cross(){
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
crossedNum++;
System.out.println(Thread.currentThread().getName()+" have Crossed the tunnel !"+" This is "+crossedNum+"th");
}
}