zoukankan      html  css  js  c++  java
  • ScheduledThreadPoolExecutor Usage

    refs:

     https://blog.csdn.net/wenzhi20102321/article/details/78681379

    对比一下Timer和ScheduledThreadPoolExecutor:

    TimerScheduledThreadPoolExecutor
    单线程 多线程
    单个任务执行时间影响其他任务调度 多线程,不会影响
    基于绝对时间 基于相对时间
    一旦执行任务出现异常不会捕获,其他任务得不到执行 多线程,单个任务的执行不会影响其他线程

    所以,在JDK1.5之后,应该没什么理由继续使用Timer进行任务调度了。

    ScheduledThreadPoolExecutor usage: 
    ScheduledThreadPoolExecutor scheduled = new ScheduledThreadPoolExecutor(2);
    scheduled.scheduledAtFixedRate(new Runnable() {
        @Override
        public void run() {
            Log.e("wegeh");
        }
    }, 0, 40, TimeUnit.MILLISECONDS);  // 0表示首次执行任务的延迟时间,40表示每次执行任务的间隔时间,
    TimeUnit.MILLISECONDS执行的时间间隔数值单位

    ScheduledThreadPoolExecutor的使用

    下面用一个具体的例子来说明ScheduledThreadPoolExecutor的使用:

    public class ScheduledThreadPoolTest {
    
        public static void main(String[] args) throws InterruptedException {
            // 创建大小为5的线程池
            ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5);
    
            for (int i = 0; i < 3; i++) {
                Task worker = new Task("task-" + i);
                // 只执行一次
    //          scheduledThreadPool.schedule(worker, 5, TimeUnit.SECONDS);
                // 周期性执行,每5秒执行一次
                scheduledThreadPool.scheduleAtFixedRate(worker, 0,5, TimeUnit.SECONDS);
            }
    
            Thread.sleep(10000);
    
            System.out.println("Shutting down executor...");
            // 关闭线程池
            scheduledThreadPool.shutdown();
            boolean isDone;
            // 等待线程池终止
            do {
                isDone = scheduledThreadPool.awaitTermination(1, TimeUnit.DAYS);
                System.out.println("awaitTermination...");
            } while(!isDone);
    
            System.out.println("Finished all threads");
        }
    
    
    }
    
    
    class Task implements Runnable {
    
        private String name;
    
        public Task(String name) {
            this.name = name;
        }
    
        @Override
        public void run() {
            System.out.println("name = " + name + ", startTime = " + new Date());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("name = " + name + ", endTime = " + new Date());
        }
    
    }

    from api doc:

    public class CustomScheduledExecutor extends ScheduledThreadPoolExecutor {
        static class CustomTask<V> implements RunnableSchedluedFuture<V> {...}
        
        protected <V> RunnableSchedluedFuture<V> decorateTask(Runnable r, RunnableSchedluedFuture<V> task) {
            return new CustomTask<V> (r, task);
        }
    
        protected <V> RunnableSchedluedFuture<V> decorateTask(Callable<V> c, RunnableSchedluedFuture<V task) {
            return new CustomTask<V>(c, task);
        }
           // ... add constructors, etc.
    }
  • 相关阅读:
    各大云服务器的对比
    程式上传的功能修改
    如何免费拥有一个聊天机器人
    自学网站大全(值得收藏)
    三菱PLC串口通信的IO控制
    免费下载知网、万方等数据库文档教程
    QT--Android之全配置教程
    QT--Android之Android环境配置
    QT--Android之Java环境配置
    安装纯净的Windows或者Ubuntu系统教程
  • 原文地址:https://www.cnblogs.com/bluestorm/p/10550978.html
Copyright © 2011-2022 走看看