`
qtlkw
  • 浏览: 307223 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

ExecutorService VS Timer

阅读更多
I have code where I schedule task using java.util.timer. I was looking around and saw ExecutorService can do the same. So this question here, have you used Timer and ExectuorService to schedule tasks , what is the benefit of one using over another.

Also wanted to check if anyone has uses the Timer class and ran into any issues which the ExecutorService solved it for them.

    * Timer can be sensitive to changes in the system clock, ScheduledThreadPoolExecutor isn't
    * Timer has only one execution thread, so long-running task can delay other tasks. ScheduledThreadPoolExecutor can be configured with any number of threads. Furthermore, you have full control over created threads, if you want (by providing ThreadFactory)
    * runtime exceptions thrown in TimerTask kill that one thread, thus making Timer dead :-( ... i.e. scheduled tasks will not run anymore. ScheduledThreadExecutor not only catches runtime exceptions, but it lets you handle them if you want (by overriding afterExecute method from ThreadPoolExecutor). Task which threw exception will be canceled, but other tasks will continue to run.

If it's available to you, then it's difficult to think of a reason not to use the Java 5 executor framework. Calling:

ScheduledExecutorService ex = Executors.newSingleThreadScheduledExecutor();


will give you a ScheduledExecutorService with similar functionality to Timer (i.e. it will be single-threaded) and but whose access may be slightly more scalable (under the hood, it uses concurrent structures rather than complete synchronization as with the Timer class). Using a ScheduledExecutorService also gives you advantages such as:

    * you can customise it if need be (see the newScheduledThreadPoolExecutor() or the ScheduledThreadPoolExecutor class)
    * the 'one off' executions can return results

About the only reasons for sticking to Timer I can think of are:

    * it is available pre Java 5
    * a similar class is provided in J2ME, which could make porting your application easier (but it wouldn't be terribly difficult to add a common layer of abstraction in this case)

An ExecutorService may be a thread pool, or even spread out across other systems in a cluster and do things like one-off batch execution, etc...

ScheduledExecutorService ses =
            Executors.newScheduledThreadPool(1);
分享到:
评论

相关推荐

    taskTimer 线程 任务

    5. **线程池**:`ExecutorService`和`ThreadPoolExecutor`允许创建线程池,它可以有效地管理和控制线程数量,避免频繁创建和销毁线程带来的开销,提高系统性能。 总之,无论是`Timer`的定时任务还是线程管理,都是...

    几种定时任务(Timer、TimerTask、ScheduledFuture)的退出—结合真实案例【JAVA并发】.docx

    ScheduledExecutorService基于ExecutorService,可以调度延迟执行或定期执行的任务。在案例中,使用了Executors.newScheduledThreadPool()初始化了一个线程池: ```java ScheduledExecutorService ...

    Java定时器Timer简述共8页.pdf.zip

    虽然Java定时器可以用来调度任务,但它不同于Java的ExecutorService或ScheduledExecutorService。后者是Java并发API的一部分,提供了更高级的线程管理和任务调度功能,如更灵活的定时策略,支持取消和中断任务等。 ...

    jwood-standup-timer_java_

    4. **多线程**:为了保证用户界面的响应性,计时器和提醒功能可能会在单独的线程中运行,利用Java的并发库如`Thread`或`ExecutorService`。 5. **配置管理**:应用可能允许用户自定义默认的发言时间和其他设置,...

    定时响的闹钟

    通过`Timer`和`TimerTask`可以设置定时触发事件,而使用`Thread`或`ExecutorService`可以在后台线程上执行音频播放,避免影响主程序的运行。这个项目为学习和实践Java并发编程提供了很好的实例。

    多线程同步,互斥,联合,守护,计时器线程Timer

    Java的`ExecutorService`和`ThreadPoolExecutor`允许我们管理一组可重用的线程,提高系统效率并控制并发级别。Example9_11.java和Example9_16.java可能展示了如何创建和使用线程池。 通过研究这些例子,初学者可以...

    java 实现调度器

    `ScheduledExecutorService`是`ExecutorService`接口的一个子接口,提供了更高级的定时和延迟执行任务的能力。与`Timer`相比,`ScheduledExecutorService`是线程安全的,更适合多线程环境。它可以处理多个并发任务,...

    java定时器+多线程(池)+java队列Demo

    Java编程语言在处理并发任务和时间调度方面提供了丰富的工具,如`java.util.Timer`、线程池和队列。在“java定时器+多线程(池)+java队列Demo”这个项目中,我们将深入探讨这三个核心概念。 1. **Java定时器(java....

    java定时器\多线程(池)\java队列Demo

    Timer timer = new Timer(); TimerTask task = new TimerTask() { @Override public void run() { // 执行任务的代码 } }; timer.schedule(task, delay, period); // 延迟delay毫秒后开始,然后每隔period毫秒...

    java定时器

    - 当主线程结束时,定时器也会自动终止,因此在某些情况下可能需要将定时器的生命周期绑定到特定的上下文,比如使用`ExecutorService`。 - 避免在`TimerTask`的`run`方法中抛出未捕获的异常,这会导致`Timer`线程...

    java通过线程控制程序执行超时

    本篇将深入探讨如何利用Java的线程和定时器(Timer)来实现这个功能。 首先,Java中的线程是并发编程的基础。通过创建和启动线程,我们可以让多个任务同时进行,提高程序的执行效率。线程主要有`Thread`类和`...

    Java调度原理及使用

    为了解决`Timer`的问题,Java 5引入了`ScheduledExecutorService`,它是`ExecutorService`的一个扩展,支持定时和周期性的任务调度。`ScheduledExecutorService`基于线程池模型,每个任务都由单独的线程执行,因此...

    StopWatch-Timer

    此外,`ExecutorService`和`Future`接口可以用于更高级的线程管理和任务调度。 2. **定时器类**:Java标准库提供了`java.util.Timer`和`java.util.concurrent.Timer`类,可以用于安排未来的任务执行。然而,由于...

    java.util.concurrent.uml.pdf

    ExecutorService是Executor的一个子接口,增加了submit方法,允许提交Callable和Runnable任务,并返回一个Future对象,表示异步计算的结果。submit方法有两种形式,一种接受Runnable参数并返回一个Future,另一种...

    java多线程和定时器学习

    在实际开发中,还需要注意线程安全的数据结构,如`ConcurrentHashMap`、`CopyOnWriteArrayList`等,以及线程池的使用,如`ExecutorService`、`ThreadPoolExecutor`等,它们可以提高系统效率并避免资源浪费。...

    Java计划技术

    为了克服`Timer`类的限制,Java 5引入了`ScheduledExecutorService`接口,它是`ExecutorService`的扩展,提供了更强大和线程安全的计划任务功能。`ScheduledThreadPoolExecutor`是实现`ScheduledExecutorService`的...

    JAVA定时器

    `java.util.concurrent.ScheduledExecutorService` 是Java并发包中的定时任务工具,它是`ExecutorService`的一个子接口,提供了更强大和灵活的定时任务管理功能。可以通过`Executors`类的静态方法如`...

    java Schedule

    为了解决`Timer`类的这些问题,Java 5引入了`java.util.concurrent.ScheduledExecutorService`接口,它是`ExecutorService`的子接口,提供了更高级别的定时任务调度功能。`ScheduledThreadPoolExecutor`是实现这个...

    java并发库学习笔记

    4. `newScheduledThreadPool(int corePoolSize)`:创建一个定时及周期性任务的线程池,可以用来替代`Timer`类。它可以定期执行或在特定延迟后执行任务,适合需要定时任务的场景。 线程池的生命周期有三个阶段:运行...

    关于ActionBar的例子(包括多线程)

    5. ExecutorService:Java并发框架的一部分,提供线程池服务,可以更有效地管理和复用线程。 【定时器(Timer)】 在Android中,定时器(Timer)常用于在特定时间间隔执行某些任务。主要涉及到两个类:`java.util....

Global site tag (gtag.js) - Google Analytics