用信号量实现一个线程安全的Set

package com.ljq.mydemo.thread.semaphore;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.Semaphore;

/**
 * 使用Semaphore 为容器设置 边界值
 *
 *
 * @author gino
 * 2021-11-16
 */
public class BoundHashSet<T> {
    private final Set<T> set;
    private final Semaphore semaphore;

    public BoundHashSet(Integer bound) {
        set = Collections.synchronizedSet(new HashSet<T>());
        semaphore = new Semaphore(bound);
    }

    /**
     * 增加数据
     *
     * @param o
     * @return
     * @throws InterruptedException
     */
    public boolean add(T o) throws InterruptedException {
        //申请信号量
        semaphore.acquire();
        boolean result = false;
        //记录结果
        try {
            result = set.add(o);
            return result;
        } finally {
            if (!result) {
                //释放信号量
                semaphore.release();
            }
        }

    }

    /**
     * 获取Set
     * 不要直接返回Set 这是不安全的发布
     *
     * @return
     */
    public Set<T> getSet() {
        return Collections.synchronizedSet(set);
    }

    /**
     * 移除元素
     *
     * @param obj
     * @return
     */
    public boolean remove(Object obj) {
        boolean remove = false;
        remove = set.remove(obj);
        //移除成功信号量减一
        if (remove) {
            //释放信号量
            semaphore.release();
        }
        return remove;
    }


    public static void main(String[] args) throws InterruptedException {
        BoundHashSet hashSet = new BoundHashSet<Integer>(5);
        Thread thread1 = new Thread(() -> {
            for (int i = 0; i < 5; i++) {
                try {
                    System.out.println("add the thread1 of " + i + " run:" + hashSet.add(i));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        thread1.start();

        Thread thread2 = new Thread(() -> {
            for (int i = 9; i < 14; i++) {
                try {
                    System.out.println("add the thread2 of " + i + " run:" + hashSet.add(i));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        thread2.start();

        Thread thread3 = new Thread(() -> {
            for (int i = 0; i < 9; i++) {
                System.out.println("remove the thread3 of " + i + " run:" + hashSet.remove(i));
            }
        });
        thread3.start();

        System.out.println("output result==========================");
        Set<Integer> set = hashSet.getSet();
        set.forEach(e->{
            System.out.println(e);
        });
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值