@Test public void semaphore() { SemaphoreTry.semaphore(); }
static void semaphore() { ExecutorService executorService = Executors.newCachedThreadPool(); for (int i = 0; i < 10; i++) { executorService.execute(() -> { try { semaphore.acquire(); } catch (InterruptedException e) { throw new RuntimeException(e); } System.out.println(Thread.currentThread().getName() + " get the semaphore " + " now have " + (3 - semaphore.availablePermits()) + " running..."); System.out.println(Thread.currentThread().getName() + " will leave..."); semaphore.release(); System.out.println("all of the thread is " + (3 - semaphore.availablePermits())); }); } executorService.shutdown(); }
SemaphoreTry { static void semaphore() { ExecutorService executorService = Executors.newCachedThreadPool(); for (int i = 0; i < 10; i++) { executorService.execute(() -> { try { semaphore.acquire(); } catch (InterruptedException e) { throw new RuntimeException(e); } System.out.println(Thread.currentThread().getName() + " get the semaphore " + " now have " + (3 - semaphore.availablePermits()) + " running..."); System.out.println(Thread.currentThread().getName() + " will leave..."); semaphore.release(); System.out.println("all of the thread is " + (3 - semaphore.availablePermits())); }); } executorService.shutdown(); } }
SemaphoreTry { static void semaphore() { ExecutorService executorService = Executors.newCachedThreadPool(); for (int i = 0; i < 10; i++) { executorService.execute(() -> { try { semaphore.acquire(); } catch (InterruptedException e) { throw new RuntimeException(e); } System.out.println(Thread.currentThread().getName() + " get the semaphore " + " now have " + (3 - semaphore.availablePermits()) + " running..."); System.out.println(Thread.currentThread().getName() + " will leave..."); semaphore.release(); System.out.println("all of the thread is " + (3 - semaphore.availablePermits())); }); } executorService.shutdown(); }  }
SemaphoreTry { static void semaphore() { ExecutorService executorService = Executors.newCachedThreadPool(); for (int i = 0; i < 10; i++) { executorService.execute(() -> { try { semaphore.acquire(); } catch (InterruptedException e) { throw new RuntimeException(e); } System.out.println(Thread.currentThread().getName() + " get the semaphore " + " now have " + (3 - semaphore.availablePermits()) + " running..."); System.out.println(Thread.currentThread().getName() + " will leave..."); semaphore.release(); System.out.println("all of the thread is " + (3 - semaphore.availablePermits())); }); } executorService.shutdown(); }   }
SemaphoreTry { static void semaphore() { ExecutorService executorService = Executors.newCachedThreadPool(); for (int i = 0; i < 10; i++) { executorService.execute(() -> { try { semaphore.acquire(); } catch (InterruptedException e) { throw new RuntimeException(e); } System.out.println(Thread.currentThread().getName() + " get the semaphore " + " now have " + (3 - semaphore.availablePermits()) + " running..."); System.out.println(Thread.currentThread().getName() + " will leave..."); semaphore.release(); System.out.println("all of the thread is " + (3 - semaphore.availablePermits())); }); } executorService.shutdown(); }    }