Hatena::ブログ(Diary)

hiro_saの日記

2012-11-10

セマフォ

現場で出てきたのでメモ。

以下の本で勉強させて頂きました。
⬇⬇




セマフォスレッドの同時実行数を制御してみる。

スレッド数管理クラス


public class ControlThreadNum {
    private final Semaphore semaphore;
    public ControlThreadNum(int PermitNum) {
        this.semaphore = new Semaphore(PermitNum);
    }
    public int getPermitNum() {
        return this.semaphore.availablePermits();
    }
    public void doThread() {
        try {
            this.semaphore.acquire();
            System.out.println("acquire - Current PermitNum : " + getPermitNum());
            System.out.println("acquire - CurrentThreadName : " + Thread.currentThread().getName());
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            System.out.println("Error Occurred!!");
        } finally {
            this.semaphore.release();
            System.out.println("release - CurrentThreadName : " + Thread.currentThread().getName());
            System.out.println("release - Current PermitNum : " + getPermitNum());
        }
    }
}


・実行スレッドクラス

public class RunThread extends Thread {
    private final ControlThreadNum controlThreadNum;
    public RunThread(ControlThreadNum controlThreadNum) {
        this.controlThreadNum = controlThreadNum;
    }
    @Override
    public void run() {
        controlThreadNum.doThread();
    }
}


・メインクラス

public class ThreadDriver {
    public static void main(String[] args) {
        ControlThreadNum controlThreadNum = new ControlThreadNum(5);
        for (int i = 0; i < 10; i++) {
            new RunThread(controlThreadNum).start();
        }
    }
}


実行結果

acquire - Current PermitNum : 4
acquire - CurrentThreadName : Thread-1
acquire - Current PermitNum : 3
acquire - CurrentThreadName : Thread-2
acquire - Current PermitNum : 2
acquire - CurrentThreadName : Thread-3
acquire - Current PermitNum : 1
acquire - CurrentThreadName : Thread-4
acquire - Current PermitNum : 0
acquire - CurrentThreadName : Thread-5
release - CurrentThreadName : Thread-3
acquire - Current PermitNum : 0
acquire - CurrentThreadName : Thread-7
acquire - Current PermitNum : 2
release - CurrentThreadName : Thread-2
release - CurrentThreadName : Thread-1
release - Current PermitNum : 0
release - Current PermitNum : 0
acquire - CurrentThreadName : Thread-6
release - Current PermitNum : 0
acquire - Current PermitNum : 1
acquire - Current PermitNum : 0
acquire - CurrentThreadName : Thread-9
acquire - Current PermitNum : 0
release - CurrentThreadName : Thread-4
release - CurrentThreadName : Thread-5
release - Current PermitNum : 0
acquire - CurrentThreadName : Thread-10
acquire - CurrentThreadName : Thread-8
release - Current PermitNum : 0
release - CurrentThreadName : Thread-6
release - Current PermitNum : 3
release - CurrentThreadName : Thread-9
release - Current PermitNum : 3
release - CurrentThreadName : Thread-7
release - Current PermitNum : 3
release - CurrentThreadName : Thread-10
release - CurrentThreadName : Thread-8
release - Current PermitNum : 5
release - Current PermitNum : 5



ログだと解りづらいが、処理を実行するとpermitが0になった時点で
以降のスレッドは待ち状態になり、releaseされたら直ぐに
他のスレッドがacquireメソッドから抜ける、という動きになる。

待ち状態になっていたスレッドがどの順番で処理が再開されるかは定まっていない。

Connection: close