yuyee

          同步包中的定時器

          ScheduledExecutorService 利用線程池進行調度任務,內部使用一個DelayedWorkQueue實現,返回ScheduledFuture,而DelayQueue是用優先級隊列PriorityQueue實現的一個阻塞隊列,優先隊列的比較基準值是時間
            private static class DelayedWorkQueue 
                  extends AbstractCollection<Runnable> 
                  implements BlockingQueue<Runnable> {
                  
                  private final DelayQueue<ScheduledFutureTask> dq = new DelayQueue<ScheduledFutureTask>();
          }

          public class DelayQueue<E extends Delayed> extends AbstractQueue<E>
              implements BlockingQueue<E> {

              private transient final ReentrantLock lock = new ReentrantLock();
              private transient final Condition available = lock.newCondition();
              private final PriorityQueue<E> q = new PriorityQueue<E>();

          下面為一個小例:
          public class ConcurrentTimer {
          public static void main(String[] args) {
          new ConcurrentTimer().getScheduledExecutorService();
          }

          public void getScheduledExecutorService() {
          ScheduledExecutorService service = Executors.newScheduledThreadPool(10);
          service.scheduleAtFixedRate(new Command(), 1, 1, TimeUnit.SECONDS);
          }

          public class Command implements Runnable {

          public void run() {
          System.out.println("Command");
          }

          }
          }

          posted on 2010-11-01 15:48 羔羊 閱讀(191) 評論(0)  編輯  收藏 所屬分類: concurrent

          主站蜘蛛池模板: 武平县| 桐庐县| 延津县| 永宁县| 子洲县| 安化县| 嫩江县| 宜川县| 辽宁省| 中卫市| 通榆县| 旬阳县| 阿拉尔市| 德保县| 特克斯县| 榆社县| 扬中市| 新巴尔虎右旗| 霍邱县| 界首市| 遂平县| 潜江市| 安陆市| 胶州市| 正镶白旗| 缙云县| 朝阳区| 文山县| 安陆市| 乌拉特中旗| 修武县| 汾西县| 泽普县| 杭州市| 万载县| 湖州市| 嘉兴市| 丰镇市| 锦屏县| 青龙| 常山县|