juc包之Semaphore使用

一、好言

最好的生活狀態(tài)不過(guò)就是:一個(gè)人,安靜而豐盛;兩個(gè)人,溫暖而踏實(shí)。

二、背景

看《Java并發(fā)編程實(shí)戰(zhàn)》,第五章,代碼練習(xí)記載下

三、內(nèi)容

package com.mouse.moon.threaddemo;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.Semaphore;
/**
 * Created by Mahone Wu on 2017/6/16.
 */
public class BoundHashSet<T> {
    private  final Set<T> set ;
    private final Semaphore semaphore;
    public BoundHashSet(int bound){
        this.set = Collections.synchronizedSet(new HashSet<T>());
        semaphore = new Semaphore(bound);
    }
    public boolean add(T o) throws  InterruptedException{
        semaphore.acquire();//獲取許可
        boolean wasAdd = false;
        try {
            wasAdd = set.add(o);
            return wasAdd;
        }finally {
            if(!wasAdd){
                semaphore.release();
            }
        }
    }
    public boolean remove(Object o){
        boolean wasRemoved = set.remove(o);
        if(wasRemoved){
            semaphore.release();//釋放許可
        }
        return wasRemoved;
    }
}

package com.mouse.moon.threaddemo;
/**
 * Created by Mahone Wu on 2017/6/16.
 */
public class Person {
    private String name;
    private String address;
    public Person(String name, String address) {
        this.name = name;
        this.address = address;
    }
}
package com.mouse.moon.threaddemo;

/**
 * Created by Mahone Wu on 2017/6/16.
 */
public class Main {
    public static void main(String[] args)  {

        final Integer permit = 3;

        Person p1 = new Person("a", "a");
        Person p2 = new Person("b", "b");
        Person p3 = new Person("c", "c");
        Person p4 = new Person("d", "d");
        Person p5 = new Person("e", "e");
        BoundHashSet bhs = new BoundHashSet(permit);
        boolean f1 = false;
        try {
            f1 = bhs.add(p1);
        } catch (InterruptedException e) {
            System.out.println("1");
            e.printStackTrace();
        }
        System.out.println(f1);
        boolean f2 = false;
        try {
            bhs.remove(p1);//這里釋放,可以自動(dòng)修改許可permit大小
            f2 = bhs.add(p2);
        } catch (InterruptedException e) {
            System.out.println("2");
            e.printStackTrace();
        }
        System.out.println(f2);

        boolean f3 = false;
        try {
            f3 = bhs.add(p3);
        } catch (InterruptedException e) {
            System.out.println("3");
            e.printStackTrace();
        }
        System.out.println(f3);

        boolean f4 = false;
        try {
            f4 = bhs.add(p4);
        } catch (InterruptedException e) {
            System.out.println("4");
            e.printStackTrace();
        }
        System.out.println(f4);

        boolean f5 = false;
        try {
            f5 = bhs.add(p5);
        } catch (InterruptedException e) {
            System.out.println("5");
            e.printStackTrace();
        }
        System.out.println(f5);
    }
}

可以看見(jiàn)打印了4個(gè)true,并且程序還處理執(zhí)行中,等待釋放一直阻塞到有為至。


image.png
image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容