Javaの並行プログラミング、綿密な探査JUC - AQS

java.util.concurrentの(JUC)が大幅に同時の性能を向上させる、AQSはJUCの中核であると考えられます。

たCountDownLatch

複数のスレッドを待っているスレッドを制御するために使用します。

それはゼロに減少するとき、各コールカウントダウン()メソッドは、これらのコールが待つため()メソッドは、待機中のスレッドに目覚めされ、カウンタ値マイナス1を行いますカウンタcntを維持します。


V2-ff7c71246e31eb60e3f0d7d1f34132e1_hd.jpg



パブリッククラスCountdownLatchExample { 

    公共の静的な無効メイン(文字列[]引数)が例外:InterruptedExceptionをスロー{ 
        最終INT totalThread = 10。
        たCountDownLatchたCountDownLatch =新たCountDownLatch(totalThread)。
        ExecutorServiceのExecutorServiceの= Executors.newCachedThreadPool()。
        以下のために(INT i = 0; iは<totalThread; iは++){ 
            executorService.execute(() - > { 
                System.out.print( "実行.."); 
                countDownLatch.countDownを(); 
            }); 
        } 
        countDownLatch.await()。
        System.out.println( "終了"); 
        executorService.shutdown(); 
    } 
}
run..run..run..run..run..run..run..run..run..run..end

CyclicBarrierを

複数のスレッドが到着した場合にのみ、複数のスレッドがお互いを待つ制御するために使用し、これらのスレッドは実行を継続します。

そして、類似したCountDownLatchは、メンテナンスカウンタによって達成されます。スレッドは()メソッドは、1カウンタと、カウンタがゼロになるまで待機します後、スレッド内のすべてのコールを待つ()メソッドは待機し続ける実行を待ちます。

1つの違いはCyclicBarrierをして​​たCountDownLatch、CyclicBarrierをカウンタをリセット()メソッドを呼び出すことにより、リサイクルすることができ、それが循環障壁と呼ばれていたです。

CyclicBarrierをタイム当事者はカウンタの初期値、barrierActionすべてのスレッドがバリアを一度に実行されます達していることを示している2つのコンストラクタを持っています。

公共CyclicBarrierを(パーティーint型、RunnableをbarrierAction){ 
    場合(パーティー<= 0)(新しいIllegalArgumentExceptionをスローし)。
    this.parties =政党。
    this.count =政党。
    this.barrierCommand = barrierAction。
} 

(当事者INT)公衆CyclicBarrierを{ 
    この(パーティー、NULL); 
}


v2-38bcb3e439af11c0c3cf94e3a02829c9_hd.jpg



public class CyclicBarrierExample {

    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; i++) {
            executorService.execute(() -> {
                System.out.print("before..");
                try {
                    cyclicBarrier.await();
                } catch (InterruptedException | BrokenBarrierException e) {
                    e.printStackTrace();
                }
                System.out.print("after..");
            });
        }
        executorService.shutdown();
    }
}
before..before..before..before..before..before..before..before..before..before..after..after..after..after..after..after..after..after..after..after..

Semaphore

Semaphore 类似于操作系统中的信号量,可以控制对互斥资源的访问线程数。


v2-48bed7aaeeb9ccdb5e550fc406d65f0d_hd.jpg



以下代码模拟了对某个服务的并发请求,每次只能有 3 个客户端同时访问,请求总数为 10。

public class SemaphoreExample {

    public static void main(String[] args) {
        final int clientCount = 3;
        final int totalRequestCount = 10;
        Semaphore semaphore = new Semaphore(clientCount);
        ExecutorService executorService = Executors.newCachedThreadPool();
        for (int i = 0; i < totalRequestCount; i++) {
            executorService.execute(()->{
                try {
                    semaphore.acquire();
                    System.out.print(semaphore.availablePermits() + " ");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    semaphore.release(); 
                } 
            })。
        } 
        executorService.shutdown()。
    } 
}


2 1 2 2 2 2 2 1 2 2
フリーのJavaは、Java、Redisの、MongoDBのは、MySQL、カバーし、独自の高度なデータ収集を必要とする飼育係、春の雲を、ダボは、30Gの合計を高い並行性と他のチュートリアルを配布しました。
ポータル: mp.weixin.qq.com/s/Jzdd


おすすめ

転載: blog.51cto.com/13672983/2402041