import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
public class TestSemaPhore {
public static void main(String[] args) {
ExecutorService pool = Executors.newFixedThreadPool(10);
final Semaphore s = new Semaphore(5);
for (int i = 0; i < 10; i++) {
pool.execute(new Thread(new Runnable() {
@Override
public void run() {
try {
s.acquire();
System.out.println("saved"+Thread.currentThread().getName());
Thread.sleep(5000);
s.release();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
},""+i));
}
pool.shutdown();
}
}