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

顺序队列实现任务以此执行-任务调度系列2

时间:2015-01-20 23:38:07      阅读:257      评论:0      收藏:0      [点我收藏+]

标签:

顺序队列实现任务以此执行的想法:

public class TaskManage {

    //任务队列
    private BlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(10); 
     
    private boolean running = false;  
      
    public void start() {  
        running = true;  
        Thread t = new Thread(new OrderedJob());  
        t.start();  
    }  
  
    public void stop() {  
        running = false;  
    }  
    
    public void submit(Runnable task){
        try {
            if(queue.offer(task, 5000L, TimeUnit.MILLISECONDS) == false){
                System.out.println();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    class OrderedJob implements Runnable {  
          
        @Override  
        public void run() {  
            while (running) {  
                try {  
                    Runnable job = queue.poll(5000L, TimeUnit.MILLISECONDS);  
                    try {  
                        if (job != null)  
                            job.run();  
                    } catch (RuntimeException e) {  
                        // TODO: handle exception  
                    }  
                } catch (InterruptedException e) {  
                    e.printStackTrace();  
                }  
            }  
        }  
    }
}

 

顺序队列实现任务以此执行-任务调度系列2

标签:

原文地址:http://www.cnblogs.com/killbug/p/4237684.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!