美文网首页
ExecutorService_ScheduledThreadP

ExecutorService_ScheduledThreadP

作者: perfect_jimmy | 来源:发表于2017-01-03 16:29 被阅读0次
    public class ScheduledThreadPool {
        public static void main(String[] args){
            ScheduledExecutorService  schedulePool = Executors.newScheduledThreadPool(1);
            //3秒后执行
            schedulePool.schedule(new Runnable(){
                @Override
                public void run() {
                    System.out.println("爆炸--"+System.currentTimeMillis());
                }
            },3,TimeUnit.SECONDS);
            //3秒后执行且每隔1秒执行
            schedulePool.scheduleAtFixedRate(new Runnable(){
                @Override
                public void run() {
                    System.out.println("爆炸=="+System.currentTimeMillis());
                }
            },3,1,TimeUnit.SECONDS);
        }
    }
    

    ScheduledThreadPool可以定时的或延时的执行任务

    爆炸--1483421657158
    爆炸==1483421657158
    爆炸==1483421658159
    爆炸==1483421659159
    爆炸==1483421660159
    爆炸==1483421661159
    爆炸==1483421662159
    爆炸==1483421663159
    

    相关文章

      网友评论

          本文标题:ExecutorService_ScheduledThreadP

          本文链接:https://www.haomeiwen.com/subject/uctnvttx.html