Java併発-同期器Semaphore,CountDownLatch


Java concurrentパケットには様々な同期器があります。ここでその2つをシミュレートします。信号量(Semaphore)とカウントビン(Count DownLatch)
一、 Semaphore同期器
特徴:
1.クラシックな信号量は、共有リソースへのアクセスをカウンタで制御します。
2.Semaphore(int count):count個のライセンスを持つ信号量を作成する
3.acquire()/acquire(int num):1/num個のライセンスを取得する
4.release/release(int num):1/numのライセンスをリリースする 
このプログラミングモデルを以下のコードでシミュレートします。
package com.concurrent.src;

import java.util.concurrent.Semaphore;

/**
 *        (         ,        ,                 )
 * @author admin
 *
 */
public class Ademo {

	public static void main(String[] args) {
		
		//          (  )
		Semaphore semaphore = new Semaphore(2);
		
		//  A,B,C    
		Person p1 = new Person(semaphore,"A");
		p1.start();
		
		Person p2 = new Person(semaphore,"B");
		p2.start();
		
		Person p3 = new Person(semaphore,"C");
		p3.start();
	}
}

class Person extends Thread {
	private Semaphore semaphore;
	
	public Person(Semaphore semaphore,String name) {
		setName(name);
		this.semaphore = semaphore;
	}
	
	@Override
	public void run() {
		System.out.println(getName() + " is waiting....");
		try {
			//      
			semaphore.acquire();
			System.out.println(getName() + " is servicing ....");
			//     
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println(getName() + " is done!");
		//      
		semaphore.release();
	}
}
実行結果:
B is waiting....
B is servicing ....
A is waiting....
A is servicing ....
C is waiting....
B is done!
C is servicing ....
A is done!
C is done!
二、 CountDown Latch同期器
特徴:
1.指定された数のイベントが発生してから運転を続けることができます。(例えば、競走試合、審判が3、2、1を呼んでからみんなで同時に走る)
2. Count DownLatch(int count):count個数が発生しなければラッチを開けられません。
3.await:ラッチ待ち
4.カウントダウン:トリガイベント
以下は計数栓のプログラミングモデルです。
package com.concurrent.src;

import java.util.concurrent.CountDownLatch;

/**
 *         
 * @author admin
 *
 */
public class Bdemo {
	public static void main(String[] args) {
		//        ,    
		CountDownLatch countDownLatch = new CountDownLatch(3);
		//  A,B,C    
		new Racer(countDownLatch, "A").start();
		new Racer(countDownLatch, "B").start();
		new Racer(countDownLatch, "C").start();
		
		//    
		for (int i = 0; i < 3; i++) {
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println(3-i);
			//         -1
			countDownLatch.countDown();
			
			if(i == 2) {
				System.out.println("start.....");
			}
		}
	}
}

class Racer extends Thread {
	private CountDownLatch countDownLatch;
	
	public Racer(CountDownLatch countDownLatch,String name) {
		setName(name);
		this.countDownLatch = countDownLatch;
	}
	
	@Override
	public void run() {
		System.out.println(getName() + " is waiting....");
		try {
			//      
			countDownLatch.await();;
			for (int i = 0; i < 3; i++) {
				System.out.println(getName() + " : " + i);
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
実行結果:
A is waiting....
C is waiting....
B is waiting....
3
2
1
start.....
A : 0
A : 1
A : 2
C : 0
C : 1
C : 2
B : 0
B : 1
B : 2