Online similar to a lot of articles, not to say, directly on the code:
[java] View plain copy/** * flow control (semaphore implementation) * * @ author ln * */ public class flowcontrol { /** * Maximum traffic */ private static final int max_ access_count = 20; /** * can only have max_access_count number of threads simultaneous access */ private static final semaphore semaphore = new semaphore (MAX_ACCESS_ COUNT); public static void main (String[] args) { // thread pool &nbsP; executorservice exec = executors.newcachedthreadpool (); // Simulation 30 clients for (int i = 0; i < 30; i++) { runnable run = new runnable () { @Override public void run () { try { &nbThe access is discarded if no permission is available within the sp; // 1 seconds. if (Semaphore.tryacquire (1, timeunit.seconds)) { &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("Executing ..."); //do some things ... thread.sleep (2 * 1000); &nbsP; system.out.println ("execution completed.") "); } else { &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("Access denied ...") "); } } catch ( Interruptedexception e) {