前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java:基于LinkedBlockingQueue实现的资源池

java:基于LinkedBlockingQueue实现的资源池

作者头像
10km
发布2019-05-25 22:22:52
1.2K0
发布2019-05-25 22:22:52
举报
文章被收录于专栏:10km的专栏10km的专栏

版权声明:本文为博主原创文章,转载请注明源地址。 https://cloud.tencent.com/developer/article/1433682

假有这样一个需求:

有一组类型为R固定数目的资源对象,多个线程在使用资源对象r时需要申请取用一个资源对象,用完后还回以便其他线程使用。

这个需求很简单,用commons-pool就可以实现,但仅为了这个需求就增加一个jar依赖,有点不划算,所以我基于LinkedBlockingQueue设计了一个资源池对象(resource pool)对象来实现这个需求。

资源池对象有两个基本的方法apply()/free()分别用于申请和释放资源。用一个LinkedBlockingQueue类型的queue来保存空闲的资源对象

  • apply() 从资源队列queue中申请一个资源,如果队列为空,线程阻塞,否则就从队列头部取出一个对象,保存在TLS变量中
  • free() 归还资源对象,将TLS变量中保存的资源对象重新加入queue尾部。

apply()/free()必须成对使用

以下是实现代码 ResourcePool.java

/**
 * 资源池管理对象<br>
 * {@link #apply()},{@link #free()}用于申请/释放资源,申请的资源对象不可跨线程调用,<br>
 * 通过重写{@link #isNestable()}方法决定是否允许嵌套调用
 * @author guyadong
 *
 * @param <R> 资源类型
 */
public class ResourcePool<R>{
    /** 资源队列 */
    protected final LinkedBlockingQueue<R> queue = new LinkedBlockingQueue<R>();
    /** 当前线程申请的资源对象 */
    private final ThreadLocal<R> tlsResource = new ThreadLocal<R>();
    /** 线程嵌套计数 */
    private final ThreadLocal<AtomicInteger> threadNestCount= new ThreadLocal<AtomicInteger>();
    private final boolean nestable = isNestable();
    protected ResourcePool() {
    }
    /**
     * 构造方法
     * @param resources 资源对象集合
     * @throws IllegalArgumentException 包含{@code null}元素
     */
    public ResourcePool(Collection<R> resources){
        for(R r:resources){
            if(null == r){
                throw new IllegalArgumentException("resources contains null element");
            }
            queue.add(r);
        }
    }
    @SafeVarargs
    public ResourcePool( R ...resources ){
        this(Arrays.asList(resources));
    }
    /**
     * 从资源队列{@link #queue}中取出一个对象,保存到{@link #tlsResource}
     * @return
     * @throws InterruptedException
     */
    private R getResource() throws InterruptedException{
        if(null != tlsResource.get()){
            // 资源状态异常
            throw new IllegalStateException("INVALID tlsResource state");
        }
        R r;
        if(queue.isEmpty() && null != (r = newResource())){
            queue.offer(r);
        }
        r = open(queue.take());
        tlsResource.set(r);
        return r;
    }
    /**
     * 将{@link #tlsResource}中的资源对象重新加入资源队列{@link #queue},并清除TLS变量{@link #tlsResource}
     */
    private void recycleResource(){
        R r = tlsResource.get();
        if(null == r){
            // 资源状态异常
            throw new IllegalStateException("INVALID tlsResource while recycle");
        }
        // 放回队例
        queue.offer(close(r));
        tlsResource.remove();
    }
    /**
     * (阻塞式)申请当前线程使用的资源对象,不可跨线程使用
     * @return
     * @throws InterruptedException
     */
    public final R applyChecked() throws InterruptedException{
        if(nestable){
            AtomicInteger count = threadNestCount.get();
            if(null == count){
                // 当前线程第一次申请资源
                count = new AtomicInteger(1);
                threadNestCount.set(count);
                return getResource();
            }else{
                // 嵌套调用时直接返回TLS变量
                if(null == this.tlsResource.get()){
                    // 资源状态异常
                    throw new IllegalStateException("INVALID tlsResource");
                }
                count.incrementAndGet();
                return this.tlsResource.get();
            }           
        }else{
            return getResource();
        }
    }
    /**
     * (阻塞式)申请当前线程使用的资源对象,不可跨线程使用<br>
     * {@link InterruptedException}封装到{@link RuntimeException}抛出
     * @return
     * @see #applyChecked()
     */
    public final R apply(){
        try {
            return applyChecked();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
    /**
     * 释放当前线程占用的资源对象,放回资源队列
     */
    public final void free(){
        if(nestable){
            AtomicInteger count = threadNestCount.get();
            if(null == count){
                // 申请/释放没有成对调用
                throw new IllegalStateException("INVALID nestCount");
            }
            if( 0 == count.decrementAndGet()){
                threadNestCount.remove();
                recycleResource();
            }           
        }else{
            recycleResource();
        }
    }
    /** 是否允许嵌套 */
    protected boolean isNestable() {
        return false;
    }
    /**
     * 创建一个新的资源对象
     * @return
     */
    protected R newResource(){
        return null;
    }
    /**
     * 资源从队形从取出时调用,子类可重写此方法
     * @param resource
     * @return 返回 {@code resource
     */
    protected R open(R resource){
        return resource;
    }
    /**
     * 资源对象放回队列时调用,子类可重写此方法
     * @param resource
     * @return 返回 {@code resource}
     */
    protected R close(R resource){
        return resource;
    }

完整代码参见gitee 代码仓库:ResourcePool.java

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018年02月07日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
SSL 证书
腾讯云 SSL 证书(SSL Certificates)为您提供 SSL 证书的申请、管理、部署等服务,为您提供一站式 HTTPS 解决方案。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档