1.用信号类semaphore
1.全部代码:
public class ThreadPoolTest {
private static Semaphore semaphore=new Semaphore(5);
public static void main(String[] args) {
for (int i = 0; i < 100; i++) {
new Thread(new Runnable() {
public void run() {
method();
}
}).start();
}
}
private static void method() {
try {
semaphore.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("ThreadName="+Thread.currentThread().getName()+"我过来了");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System