码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-01-22 18:00:47      阅读:133      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!