Java并发——ScheduledExecutorService

Java ScheduledExecutorService 使用学习 Java 中的 ScheduledExecutorService 接口用来实现延迟执行或者定时执行的任务。在阅读 RocketMQ 源码(release-4.7.1版本)的过程中,发现很多地方都是使用的 ScheduledExecutorService 来实现定时任务。比如,在 broker 启动过程,BrokerController 类中使用 scheduledExecutorService 执行 broker 启动后的定时任务,比如消息消费偏移量 offset 的持久化定时任务,大致如下... 阅读详情

ScheduledExecutorService

类 Scheduledexecutor Service的主要作用就是可以将定时任务与线程池功能结合使用。
在这里插入图片描述

ScheduledThreadPoolExecutor使用Callable延迟运行
public class MyCallableAA implements Callable<String> {
    @Override
    public String call() throws Exception {
        try {
            System.out.println("callA begin " + Thread.currentThread().getName() + " " + System.currentTimeMillis());
            Thread.sleep(3000);
            System.out.println("callA end " + Thread.currentThread().getName() + " " + System.currentTimeMillis());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "returnA";
    }
}



public class MyCallableBB implements Callable<String> {
    @Override
    public String call() throws Exception {
        System.out.println("callB begin " + Thread.currentThread().getName() + " " + System.currentTimeMillis());
        System.out.println("callB end " + Thread.currentThread().getName() + " " + System.currentTimeMillis());
        return "returnB";
    }
}


public class MyRun1 {
    public static void main(String[] args) {
        try {
            List<Callable> callables = new ArrayList<>();
            callables.add(new MyCallableAA());
            callables.add(new MyCallableBB());
            //调用方法newSingleThreadScheduledExecutor(),取得一个单任务的计划任务执行池
            ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
            ScheduledFuture<String> futureA = executorService.schedule(callables.get(0), 4L, TimeUnit.SECONDS);
            ScheduledFuture<String> futureB = executorService.schedule(callables.get(1), 4L, TimeUnit.SECONDS);

            System.out.println("     X=" + System.currentTimeMillis());
            System.out.println("返回值A:" + futureA.get());
            System.out.println("返回值B:" + futureB.get());
            System.out.println("     Y=" + System.currentTimeMillis());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}

从X到Y的运行时间为7秒,阻塞点是get()方法。
从运行结果中可以发现:public <V> Scheduled Future<V> schedule(Callable<v> callable, long delay, Time Unit unit)
方法中的第2个参数在多个任务中同时消耗时间,并不是一个任务执行完毕后再等待4秒继续执行的效果。由于第1个任务从计划任务到运行结束需要用时7秒,那么第2个任务其实是想在第4秒被执行,由于是单任务的计划任务池,所以第2个任务的执行时间被延后3秒。
在这里插入图片描述

在此实验中使用工厂类 Executors的 newSingleThreadScheduledExecutor方法来创建
ScheduledExecutorService对象,但返回的真正对象却是 Scheduledthreadpoolexecutor,因为
Scheduled Thread PoolExecutor实现了ScheduledexecutorService接口。
在这里插入图片描述

ScheduledThreadPoolExecutor使用Runnable延迟运行
public class MyRunnableAA implements Runnable {
    @Override
    public void run() {
        try {
            System.out.println("runnableA begin " + Thread.currentThread().getName() + " " + System.currentTimeMillis());
            Thread.sleep(3000);
            System.out.println("runnableA end " + Thread.currentThread().getName() + " " + System.currentTimeMillis());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

public class MyRunnableBB implements Runnable {
    @Override
    public void run() {
        System.out.println("runnableB begin " + Thread.currentThread().getName() + " " + System.currentTimeMillis());
        System.out.println("runnableB end " + Thread.currentThread().getName() + " " + System.currentTimeMillis());
    }
}

public class MyRun2 {
    public static void main(String[] args) {
        List<Runnable> list = new ArrayList<>();
        list.add(new MyRunnableAA());
        list.add(new MyRunnableBB());
        ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();

        System.out.println("     X=" + System.currentTimeMillis());
        executorService.schedule(list.get(0), 0L, TimeUnit.SECONDS);
        executorService.schedule(list.get(1), 1L, TimeUnit.SECONDS);
        System.out.println("     Y=" + System.currentTimeMillis());
    }
}

在这里插入图片描述

延迟运行并取得返回值
public class MyRun3 {
    public static void main(String[] args) {
        Callable<String> callable = new Callable<String>() {
            public String call() throws Exception {
                System.out.println("a call run =" + System.currentTimeMillis());
                return "returnA";
            }
        };

        try {
            List<Callable> list = new ArrayList<>();
            list.add(callable);
            ScheduledExecutorService exector = Executors.newSingleThreadScheduledExecutor();
            System.out.println("     X=" + System.currentTimeMillis());
            ScheduledFuture future = exector.schedule(list.get(0), 4L, TimeUnit.SECONDS);
            System.out.println(future.get() + "    A=" + System.currentTimeMillis());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

在这里插入图片描述

使用scheduleAtFixedRate()方法实现周期性执行
  • command:执行线程
  • initialDelay:初始化延时
  • period:两次开始执行最小间隔时间
  • unit:计时单位

执行任务时间大于>period预定的周期时间,也就是产生了超时的效果:

public class MyRun4 {
    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println("   begin=" + System.currentTimeMillis() +
                            " ThreadName=" + Thread.currentThread().getName());
                    Thread.sleep(4000);
                    System.out.println("   end=" + System.currentTimeMillis() +
                            " ThreadName=" + Thread.currentThread().getName());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };

        ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
        System.out.println("  X=" + System.currentTimeMillis());
        executor.scheduleAtFixedRate(runnable, 1, 2, TimeUnit.SECONDS);
        System.out.println("  Y=" + System.currentTimeMillis());
    }
}

任务执行时间(4)大于周期时间(2),所以产生超时效果,每次执行任务不再间隔2秒。
在这里插入图片描述

执行任务时间<period的时间:

public class MyRun5 {
    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                System.out.println("  begin=" + System.currentTimeMillis() +
                        " ThreadName=" + Thread.currentThread().getName());
                System.out.println("  end=" + System.currentTimeMillis() +
                        " ThreadName=" + Thread.currentThread().getName());
            }
        };

        ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
        System.out.println("  X=" + System.currentTimeMillis());
        executor.scheduleAtFixedRate(runnable, 1, 2, TimeUnit.SECONDS);
        System.out.println("  Y=" + System.currentTimeMillis());
    }
}

每次间隔2秒执行。
在这里插入图片描述

注意: scheduleAtFixedRatel方法返回的 Scheduled
Future对象无法获得返回值,也就是scheduleAtFixedRate()方法不具有获得返回值的功能,而 schedule方法却可以获得返回值。所以当使用 scheduleAtFixedRate方法实现重复运行任务的效果时,需要结合自定义 Runnable接口的实现类,不要使用FutureTask类,因为FutureTask类并不能实现重复运行的效果。

使用scheduleWithFixedDelay()方法实现周期性执行

方法 schedule With FixedDelay(的主要作用是设置多个任务之间固定的运行时间间隔。

执行任务时间大于> period定的时间

public class MyRun6 {
    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println("  begin=" + System.currentTimeMillis() +
                            " ThreadName=" + Thread.currentThread().getName());
                    Thread.sleep(4000);
                    System.out.println("  end=" + System.currentTimeMillis() +
                            " ThreadName=" + Thread.currentThread().getName());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };

        ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
        System.out.println("  X=" + System.currentTimeMillis());
        executor.scheduleWithFixedDelay(runnable, 1, 2, TimeUnit.SECONDS);
        System.out.println("  Y=" + System.currentTimeMillis());
    }
}

固定每隔2秒执行一次。
在这里插入图片描述

执行任务时间<period:

public class MyRun6 {
    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                System.out.println("  begin=" + System.currentTimeMillis() +
                        " ThreadName=" + Thread.currentThread().getName());
                System.out.println("  end=" + System.currentTimeMillis() +
                        " ThreadName=" + Thread.currentThread().getName());
            }
        };

        ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
        System.out.println("  X=" + System.currentTimeMillis());
        executor.scheduleWithFixedDelay(runnable, 1, 2, TimeUnit.SECONDS);
        System.out.println("  Y=" + System.currentTimeMillis());
    }
}

固定每隔2秒执行一次。
在这里插入图片描述

getQueue()与remove()方法

方法 getQueue()的作用是取得队列中的任务,而这些任务是未来将要运行的,正在运行的任务不在此队列中。使用 scheduleAtFixedRateO和 scheduleWithFixedDelay()两个方法实现周期性执行任务时,未来欲执行的任务都是放入此队列中。

public class MyRun7 {
    public static void main(String[] args) {
        ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(10);

        Runnable_A r1 = new Runnable_A("A");
        Runnable_A r2 = new Runnable_A("B");
        Runnable_A r3 = new Runnable_A("C");
        Runnable_A r4 = new Runnable_A("D");
        Runnable_A r5 = new Runnable_A("E");

        System.out.println(r1.hashCode());
        System.out.println(r2.hashCode());
        System.out.println(r3.hashCode());
        System.out.println(r4.hashCode());
        System.out.println(r5.hashCode());

        executor.scheduleAtFixedRate(r1, 10, 2, TimeUnit.SECONDS);
        executor.scheduleAtFixedRate(r2, 10, 2, TimeUnit.SECONDS);
        executor.scheduleAtFixedRate(r3, 10, 2, TimeUnit.SECONDS);
        executor.scheduleAtFixedRate(r4, 10, 2, TimeUnit.SECONDS);
        executor.scheduleAtFixedRate(r5, 10, 2, TimeUnit.SECONDS);

        System.out.println("");

        BlockingQueue<Runnable> queue = executor.getQueue();
        Iterator<Runnable> it = queue.iterator();
        while (it.hasNext()) {
            Runnable next = it.next();
            System.out.println("队列中的:" + next);
        }
    }
}

class Runnable_A implements Runnable {
    private String username;

    public Runnable_A(String username) {
        this.username = username;
    }

    public String getUsername() {
        return username;
    }

    @Override
    public void run() {
        System.out.println("run! username=" + Thread.currentThread().getName());
    }
}

在这里插入图片描述

public class MyRun7 {
    public static void main(String[] args) throws InterruptedException {
        ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);

        Runnable_A r1 = new Runnable_A("A");
        Runnable_A r2 = new Runnable_A("B");

        ScheduledFuture<?> future1 = executor.scheduleAtFixedRate(r1, 0, 2, TimeUnit.SECONDS);
        Thread.sleep(1000);
        ScheduledFuture<?> future2 = executor.scheduleAtFixedRate(r2, 10, 2, TimeUnit.SECONDS);
        Thread.sleep(5000);
        System.out.println(executor.remove((Runnable) future2));
        System.out.println("");

        BlockingQueue<Runnable> queue = executor.getQueue();
        Iterator<Runnable> it = queue.iterator();
        while (it.hasNext()) {
            Runnable next = it.next();
            System.out.println("队列中的:" + next);
        }
    }
}

class Runnable_A implements Runnable {
    private String username;

    public Runnable_A(String username) {
        this.username = username;
    }

    public String getUsername() {
        return username;
    }

    @Override
    public void run() {
        System.out.println("run! username=" + Thread.currentThread().getName());
    }
}

在这里插入图片描述

setExecuteExistingDelayedTasksAfterShtudownPolicy()

方法 setExecuteExisting DelayedTasksAfterShutdownPolicy()的作用是当对 ScheduledThreadPoolExecutor执行了shutdown方法时,任务是否继续运行,默认值是true,也就是
当调用了 shutdown方法时任务还是继续运行,当使用 set ExecuteExistingDelayedTasksAfterShutdownPolicy(alse)时任务不再运行。

public class MyRun8 {
    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                    System.out.println("任务执行!");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };

        ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
        executor.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
        executor.schedule(runnable, 3, TimeUnit.SECONDS);
        executor.shutdown();
        System.out.println("main end");
    }
}

在这里插入图片描述

注意:方法setExecuteExistingDelayedTasksAfterShutdownPolicy可以与 schedule()和 shutdown()方法联合使用,但 setExecuteExistingDelayedTasksAfterShutdownPolicy()方法不能与 scheduleatFixedRate()和 scheduleWithFixedDelay()方法联合使用。

那么如果想实现 shutdown关闭线程池后,池中的任务还会继续重复运行,则要将 scheduleatFixedRate和 scheduleWithFixedDelayo方法与 setContinueExistingPeriodicTasksAfterShutdownPolicy()方法联合使用。

setContinueExistingPeriodicTasksAfterShutdownPolicy()

方法 setContinueExistingPeriodicTasksAfterShutdownPolicy()传入tue的作用是当使用 scheduleAtFixedRate()方法或 scheduleWithFixedDelay()方法时,如果调用 ScheduledThreadPoolExecutor对象的 shutdown()方法,任务还会继续运行,传入false时任务不运行,进程销毁。

public class MyRun8 {
    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                    System.out.println("任务执行!");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };

        ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
        executor.setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
        executor.scheduleWithFixedDelay(runnable, 0,3, TimeUnit.SECONDS);
        executor.shutdown();
        System.out.println("main end");
    }
}

在这里插入图片描述

cancel(boolean)与setRemoveOnCancelPolicy()
  • 方法 cancel(boolean)的作用设定是否取消任务。
  • 方法 setRemove On CancelPolicy (boolean)的作用设定是否将取消后的任务从队列中清除。
public class MyRun9 {
    public static void main(String[] args) {
        ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(10);
        Runnable runnable1 = new Runnable_B("A");
        ScheduledFuture<?> future = executor.schedule(runnable1, 1, TimeUnit.SECONDS);
        System.out.println(future.cancel(true));
        System.out.println("");
        BlockingQueue<Runnable> queue = executor.getQueue();
        Iterator<Runnable> it = queue.iterator();
        while (it.hasNext()) {
            Runnable next = it.next();
            System.out.println("队列中的:" + next);
        }
        System.out.println("main end");
    }
}

class Runnable_B implements Runnable {

    private String username;

    public Runnable_B(String username) {
        this.username = username;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @Override
    public void run() {
        try {
            while (true) {
                if (Thread.currentThread().isInterrupted() == true) {
                    throw new InterruptedException();
                }
                System.out.println("run! username=" + username + " " + Thread.currentThread().getName());
                Thread.sleep(1000);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
            System.out.println("中断了!");
        }
    }
}

删除成功,但是还保留在队列中。
在这里插入图片描述

在这里插入图片描述
删除成功,从队列中剔除。
在这里插入图片描述

ScheduledExecutorService定时周期执行指定的任务 一:简单说明 ScheduleExecutorService接口中有四个重要的方法,其中scheduleAtFixedRate和scheduleWithFixedDelay在实现定时程序时比较方便。 下面是该接口的原型定义 java.util.concurrent.ScheduleExecutorService extendsExecutorService extendsExecut 阅读详情

相关推荐

BACnet - 物联网智能建筑通信协议 - 补充中

文章目录前言一、概述二、入门使用三、概念深入1 对于BACnet的一些思考2 待定总结 前言 BacNet标准 文章目录 《BACnet标准与楼宇自控系统技术》 一、概述 提示:这里可以添加本文要记录的大概内容 BACnet - 用于智能建筑的通信协议,是国际标准化组织(ISO)、美国国家标准协会(ANSI)及美国采暖、制冷与空调工程师学会定义的通信协议。BACnet针对智能建筑及控制系统的应用所设计的通信,可用在暖通空调系统(HVAC,包括暖气、通风、空气调节),也可以用在照明控制、门

. 2924

ScheduledExecutorService详解

有线程池的特性,也可以实现任务循环执行,可以看作是一个简单地定时任务组件,因为有线程池特性,所以任务之间可以多线程并发执行,互不影响,当任务来的时候,才会真正创建线程去执行我们在做一些普通定时循环任务时可以用它,比如定时刷新字典常量,只需要不断重复执行即可,这篇文章讲解一下它的用法以及注意事项,不涉及底层原理。...

qq_41563912的博客 4万+

掌握Java并发编程中的Future和ScheduledExecutorService

本文深入探讨了Java并发API中的Future和ScheduledExecutorService的使用方法和实践场景。通过对Future接口的方法详尽解析,展示了如何管理异步任务的完成情况、取消任务以及获取任务结果。同时,介绍了Callable接口与Future的结合使用,以及如何等待所有任务完成或处理超时情况。文章还探讨了任务调度的高级用法,包括单次和周期性任务的安排。

weixin_35307279的博客 468

ScheduledExecutorService - 定时周期执行任务

ScheduleExecutorService接口中有四个重要的方法,其中scheduleAtFixedRate和scheduleWithFixedDelay在实现定时程序(每隔一段时间执行一次)时比较方便。 接口中四个方法如下: 该接口中前两个方法只会执行一次,后两个方法会定时循环执行。 — 三个方法简介— 1. ScheduledFuture<?> schedule( Runnable command, long delay, T...

记录点滴 7384

深入理解Java并发编程:ScheduledExecutorService与线程池

本文详细探讨了Java并发API中ScheduledExecutorService的使用方法,包括创建和执行延迟任务、周期性任务,以及如何通过线程池提高并发效率。同时,文章也涉及了线程安全的实现以及如何在多线程环境中保护数据。

weixin_42602241的博客 408

Unity类银河战士恶魔城学习总结(P162 Improve skeleton提升敌人的效果)

为敌人添加了快慢刀

suzh1qian的博客 572

Java高并发编程中ScheduledExecutorService的使用及详细介绍-刘宇

作者:刘宇。一、什么是ScheduledExecutorService?二、ScheduledThreadPoolExecutor中的方法1、构造方法2、schedule方法3、scheduleAtFixedRate方法4、scheduleWithFixedDelay方法5、setContinueExistingPeriodicTasksAfterShutdownPolicy方法6、setExecuteExistingDelayedTasksAfterShutdownPolicy方法三、练习

刘宇的博客 2万+

保姆级教程:手把手教你用Python+Zemax仿真一个简易的Nd:YAG固体激光器谐振腔

本文提供了一份详细的Python与Zemax结合的Nd:YAG固体激光器谐振腔仿真教程。从基础理论到实战操作,涵盖晶体参数化、谐振腔设计、泵浦建模、动态仿真及热效应补偿等关键步骤,适合光学专业学生和工程师快速掌握激光器仿真技术。

weixin_30500473的博客 742

Java并发类API——ScheduledExecutorService

ScheduledExecutorService 是 Java 中用于计划和执行定时任务的接口。它扩展了 ExecutorService 接口,专门用于调度在给定的延迟后运行的任务或定期执行的任务。ScheduledExecutorService 提供了更强大的控制能力和灵活性来处理定时任务,比传统的 Timer 和 TimerTask 更加可靠和高效。它是基于线程池的调度执行器,专门用于管理和执行并发任务。类ScheduledExecutorService的主要作用就是可以将定时任务与线程池功能结合使用。

sinat_33536503的博客 2357

Centos7配置阿里云镜像源的教程

查看你的Centos版本,版本过低会导致安装失败,验证网络是否可以连接阿里云镜像,使用阿里云是需要网络的。# wget命令下载: wget [options] [url]# curl命令下载: curl [options] [url]我上面这个图片由于电脑问题是还没有加载完的。删除原来或者之前的yun源。

lyhshs的博客 2259

Python实战:5分钟搞定固定翼无人机三自由度仿真(附完整代码)

本文提供了一个基于Python的固定翼无人机三自由度仿真快速实现方案。通过面向对象设计,封装了核心运动学模型,并对比了欧拉法与龙格-库塔法两种积分方法。文章附有完整实例代码,帮助算法工程师和研究人员在5分钟内搭建轻量级仿真环境,用于快速验证控制律、路径规划等核心算法逻辑。

docker7nomad的博客 747

Java并发编程实践-ScheduledExecutorService替换Timer

《Java并发编程实践》建议将使用Timer执行延迟执行和周期执行的任务换成ScheduledExecutorService来完成。原因如下: 1)Timer使用单线程来执行所有的timer任务,效率低下; 2)由于是单线程的,所以如果其中一个timer很耗时,将会导致其他TimerTask的失效性出现问题,简单的说,如果在A任务执行的过程中,B也到执行的时间点了,但是A没有执行完,所有B会等待A...

qq_42938846的博客 867

# YOLOv11-seg改进系列 | 引入CVPR MetaFormer的C3k2_PoolingFormerCGLU模块,池化Token混合+CGLU门控MLP

本文引入 CVPR 2022 论文(MetaFormer/PoolFormer)提出的 MetaFormer 元架构设计,以Pooling Token 混合器 + ConvolutionalGLU(CGLU)MLP构成,将其封装为,用于替换 YOLOv11-seg 的 C3k2 块。对比维度C3k2(基线)C3k2_PoolingFormerCGLU(本文)参数量(n scale)2.46M≈2.49M9.6≈9.69.7(+1.0%)架构范式单阶段卷积MetaFormer 双阶段。

qq_44224801的博客 843

java并发Executor、ExecutorService、ScheduledExecutorService接口

简介:在以往创建线程时,我们通常使用new Thread()创建一个类对象,通过start()方法运行一个线程,当有多个线程,就要用上面的方法多个创建,看起来非常繁琐,代码看起来也不是非常整洁。而在JUC包下其实已经提供了相关接口(Executor、ExecutorService、ScheduledExecutorService)来作为池化技术的基础,通过Executors工具类创建各种线程池,减少每次获取资源的消耗,提高对资源的利用率。在这里主要介绍上面所说的三个接口的方法使用 一、Executor接口:

倪先生的博客 334

matlab 异常值检测与处理——Robust Z-score法【2024最新版】

博客长期更新,本文最近一次更新时间为:2024年12月9日。

点云侠的博客 1516

25 - GRACE Mascon数据缺失月份数据插值

本篇基于文章的相关内容和文章公开的奇异谱分析(SSA)插值算法程序包,对grace Mascon数据种缺失的月份进行 插值 ,总结了使用程序包的相关过程,可以将该程序应用到其他数据处理插值计算中。

weixin_43339605的博客 1842

Java 并发专题 : Timer的缺陷 用ScheduledExecutorService替代

继续并发,上篇博客对于ScheduledThreadPoolExecutor没有进行介绍,说过会和Timer一直单独写一篇Blog. 1、Timer管理延时任务的缺陷 a、以前在项目中也经常使用定时器,比如每隔一段时间清理项目中的一些垃圾文件,每个一段时间进行数据清洗;然而Timer是存在一些缺陷的,因为Timer在执行定时任务时只会创建一个线程,所以如果存在多个任务,且任务时间过长,超过了两

Hongyang 9万+

AUTOSAR CP R18-10 架构总览与导航

AUTOSAR CP R18-10 架构总览 本文档系统整理了AUTOSAR CP R18-10版本的199篇技术规范,涵盖7大架构层级: 应用层(15篇):包含车身舒适、底盘控制、HMI、安全系统、动力系统等AI应用 RTE(1篇):虚拟功能总线规范 服务层(159篇):细分为系统服务(OS/ECU管理/加密)、存储服务、通信服务(CAN/LIN/Ethernet) ECU抽象层(20篇):I/O与通信硬件抽象 MCAL(41篇):微控制器驱动层 复杂驱动(14篇):CDD设计与SPAL支撑 规范模板(2

一名留德华的,康德的信徒的,专注于汽车电子领域的博客 717

Java 并发专题 : Timer的缺陷 用ScheduledExecutorService替代

Java面试核心知识点一共有30个专题,足够读者朋友们应付面试啦,也节省朋友们去到处搜刮资料自己整理的时间!Java面试核心知识点已经有读者朋友靠着这一份Java面试知识点指导拿到不错的offer了《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!Java面试核心知识点来应付面试,借着这次机会可以送给我的读者朋友们[外链图片转存中…(img-yV7Rk2vB-1713394248347)]Java面试核心知识点。

2401_84414990的博客 686

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

Java并发之定时任务接口ScheduledExecutorService

以前在使用Timer作为定时任务时,如果同时执行多个爬虫,且执行的时间过长,超过了两个爬虫的间隔时间,就会出现一些奇怪的错误,在查找了一些资料之后,这次来尝试下ScheduledExecutorService做为定时器,下面是简单的介绍。java中要实现定时执行的方法通常有三种(这里不包括spring和一些已经实现好的工具类): 第一种就是创建一个线程然后一直循环通过sleep来达到定时执行,但是不

WILL的博客 1704

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

JAVA并发-ScheduledExecutorService

java.util.concurrent.ScheduledExecutorService是一个可以安排任务延迟执行的ExecutorService, 或者以固定的时间间隔重复执行。任务通过一个工作线程异步执行,而不是提交任务到ScheduledExecutorService的线程。 ScheduledExecutorService例子 下面是ScheduledExecutorService例子: ScheduledExecutorService scheduledExecutorServic..

成凯的专栏 842

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

Java并发 三 ScheduledExecutorService

ScheduledExecutorService是与ExecutorService类似的接口,但它可以定期执行任务。 Executor和ExecutorService的方法是在现场安排的,不会引入任何人工延迟。零或任何负值表示需要立即执行请求。 我们可以使用Runnable和Callable接口来定义任务。 public void execute() { ScheduledExecutor...

空 329

java timer并发_Java 并发专题 : Timer的缺陷 用ScheduledExecutorService替代

继续并发,上篇博客对于ScheduledThreadPoolExecutor没有进行介绍,说过会和Timer一直单独写一篇Blog.1、Timer管理延时任务的缺陷a、以前在项目中也经常使用定时器,比如每隔一段时间清理项目中的一些垃圾文件,每个一段时间进行数据清洗;然而Timer是存在一些缺陷的,因为Timer在执行定时任务时只会创建一个线程,所以如果存在多个任务,且任务时间过长,超过了两个任务的...

weixin_31993699的博客 246

Java并发系列 ScheduledExecutorService 使用

本文介绍 ScheduledExecutorService 在Java1.5以后才出现的定时任务的,在Java5之后,并发线程这块发生了根本的变化,最重要的莫过于新的启动、调度、管理线程的一大堆API了。定时任务在1.5之前是使用 Timer 来实现的,但由于 Timer 有一些问题: Timer对调度的支持是基于绝对时间,而不是相对时间的, 由此任务对系统时钟的改变是敏感的;Schedu

singleton_zw 6552
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值