首页 > 其他 > 详细

使用信号量来 限制无边界池子与队列

时间:2015-01-22 17:58:12      阅读:263      评论:0      收藏:0      [点我收藏+]

  使用信号量来 限制无边界池子与队列

 

public class BoundedExecutor {
     private final Executor exec;
     private final Semaphore semaphore;

     public BoundedExecutor(Executor exec, int bound) {
            this. exec = exec;
            this. semaphore = new Semaphore(bound);
     }

     public void submitTask( final Runnable command) throws InterruptedException {
            semaphore.acquire();
            try {
                 exec. execute(new Runnable() {
                      public void run() {
                            try {
                                command.run();
                           } finally {
                                 semaphore.release();
                           }
                     }
                });
           } catch (RejectedExecutionException e) {
                 semaphore.release();
           }
     }
}

 

使用信号量来 限制无边界池子与队列

原文:http://www.cnblogs.com/mjorcen/p/4242121.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!