java:通过Semaphore构建对象池

Semaphore

1) 正常的锁(concurrent.locks或synchronized锁)在任何时候都只允许一个任务访问一项资源,而 java.util.concurrent.Semaphore计数信号量允许n个任务同时访问该资源,可以把信号量看成是向外分发使用资源的许可证
2)对象池概念:对象池管理数量有限的对象,当要使用对象时可以签出他们,而在用户使用完毕时,可以将它们签回,这种功能可以封装在一个泛型类中:
class Pool<T>{
     private Semaphore avaliable;     //管理计数信号量
     private List<T> items = new ArrayList<T>();   //管理对象列表
     private volatile boolean[] checkOut;     //管理对象列表上相应下标的对象的签出情况
     private int size;
     
     public Pool(Class<T> classObject,int size){
          this.size = size;
          checkOut = new boolean[size];
          available = new Semaphore(size);
          for(int i=0;i<size;i++){
               try{
                    items.add(classObject.newInstance());
               }catch(Exception e){
                    throw new RunTimeException(e);
               }
          } 
     }
     public T checkOut() throw InterruptedException{   //签出对象池的信号量
          available.acquire();
          return getItem();
     }
     public T getItem(){
          for(int i=0;i<size;i++){
               if(!checkOut[i]){
                    checkOut[i] =true;
                    return item.get(i);
                }
          }
     }
     public void checkIn(T x){    //签入对象池的信号量
          if(releaseItem(x))
               available.release();
     }
     public boolean releaseItem(T x){
          int index = items.indexOf(x);
          if(index == -1)
               retrun false;
          if(checkOut[index]){
               checkOut[idnex] = false;
               return true;
          }
          return false;
     }
}

//Test
class Demo(){
}
class CheckOutWorker<T> implements Runnable{
     priavte Pool<T> pool;
     public CheckOutWorker(Pool<T> pool){ this.pool = pool; }
     public void run(){
          try{
               T item = pool.checkOut();
               <using item to do something>
               pool.checkIn(item);
          }catch(Interruptedexception ex){
          }
     }    
     /*or 记录所有已被签出的对象,返回对象列表供主程使用 
     priavte Pool<T> pool;   
     private List<T> checkOutList = new ArrayList<T>();
     public CheckOutWorker(Pool<T> pool){ this.pool = pool;this.checkOutList = checkOutList; }
     public void run(){
          try{
               T item = pool.checkOut();
               checkOutList.add(item);
               <using checkOutList to do something>
               pool.checkIn(item);
               checkOutList.remove(item);
          }catch(Interruptedexception ex){
          }
     }*/
}
class Driver{
     void main(){
          Pool<Demo> pool = new Pool(Demo.class,N);
          ExecutorService exec = Executors.newChacedTheadPool();
          for(int i=0;i<N;i++)
               exec.execute(new CheckOut(pool));
          exec.shoudown();
     }
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值