java-concurrent:aqs
Differences
This shows you the differences between two versions of the page.
java-concurrent:aqs [2019/09/10 07:05] – external edit 127.0.0.1 | java-concurrent:aqs [2022/01/13 07:50] (current) – morgan0329 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | CountDownLatch, | ||
+ | |||
+ | CountDownLatch: | ||
+ | |||
+ | CyclicBarrier: CyclicBarrier lockB = new CyclicBarrier(4); | ||
+ | |||
+ | |||
+ | Semaphore: | ||
+ | |||
+ | |||
+ | <code java> | ||
+ | public class ExampleCountDownLatch { | ||
+ | |||
+ | public static void main(String[] args) throws InterruptedException { | ||
+ | final int totalThread = 9; | ||
+ | CountDownLatch countDownLatch = new CountDownLatch(totalThread); | ||
+ | |||
+ | ExecutorService executorService = Executors.newCachedThreadPool(); | ||
+ | for(int i = 0; i< totalThread ;i ++) { | ||
+ | executorService.execute(() -> { | ||
+ | try { | ||
+ | Thread.sleep(500L); | ||
+ | System.out.println(Thread.currentThread().getName() + " run..." | ||
+ | countDownLatch.countDown(); | ||
+ | } catch(InterruptedException e) { | ||
+ | e.printStackTrace(); | ||
+ | } | ||
+ | }); | ||
+ | } | ||
+ | |||
+ | System.out.println(" | ||
+ | countDownLatch.await(); | ||
+ | System.out.println(" | ||
+ | executorService.shutdown(); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | |||
+ | public class ExampleCyclicBarrier { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | final int totalThread = 10; | ||
+ | CyclicBarrier cyclicBarrier = new CyclicBarrier(totalThread); | ||
+ | ExecutorService executorService = Executors.newCachedThreadPool(); | ||
+ | for (int i = 0; i < totalThread; | ||
+ | executorService.execute(() -> { | ||
+ | System.out.println(" | ||
+ | try { | ||
+ | cyclicBarrier.await(); | ||
+ | } catch (InterruptedException | BrokenBarrierException e) { | ||
+ | e.printStackTrace(); | ||
+ | } | ||
+ | System.out.println(" | ||
+ | }); | ||
+ | } | ||
+ | executorService.shutdown(); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | |||
+ | |||
+ | public class ExampleSemaphore { | ||
+ | |||
+ | public static void main (String[] args) { | ||
+ | final int clientCount = 3; | ||
+ | final int totalRequest = 10; | ||
+ | Semaphore semaphore = new Semaphore(clientCount); | ||
+ | |||
+ | ExecutorService executorService = Executors.newFixedThreadPool(5); | ||
+ | |||
+ | for(int i=0; i< totalRequest; | ||
+ | executorService.execute(() -> { | ||
+ | try { | ||
+ | semaphore.acquire(); | ||
+ | System.out.println(semaphore.availablePermits() + " " + Thread.currentThread().getName()); | ||
+ | Thread.sleep(7000); | ||
+ | } catch(InterruptedException e) { | ||
+ | e.printStackTrace(); | ||
+ | } finally { | ||
+ | semaphore.release(); | ||
+ | } | ||
+ | }); | ||
+ | } | ||
+ | executorService.shutdown(); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | ====== AbstractQueuedSynchronizer ====== | ||
+ | 参考:Java技术之AQS详解 | ||
+ | |||
+ | 提供了一个框架,用于实现阻塞锁和一系列依赖FIFO等待队列实现的相关同步器(如信号量,事件)。 | ||
+ | AQS为一系列同步器依赖于一个单独的原子变量(state)的同步器提供了一个非常有用的基础。子类们必须定义改变state变量的protected方法,这些方法定义了state是如何被获取或释放的。鉴于此,本类中的其他方法执行所有的排队和阻塞机制。子类也可以维护其他的state变量,但是为了保证同步,必须原子地操作这些变量。 | ||
+ | |||
+ | |||
+ | Provides a framework for implementing blocking locks and related synchronizers (semaphores, |