首页 > 其他分享 >ExecutorService VS Timer

ExecutorService VS Timer

时间:2023-09-14 12:03:18浏览次数:34  
标签:tasks Timer will VS but ScheduledExecutorService ExecutorService


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);

标签:tasks,Timer,will,VS,but,ScheduledExecutorService,ExecutorService
From: https://blog.51cto.com/u_16261339/7468627

相关文章

  • Node.js vs. Spring Boot:Hello World 性能对决,谁更快一点?
    前言:SpringBoot在Java生态中备受欢迎,它是一款基于Java构建的轻量级服务端框架,主要用于Web服务。SpringBoot的应用使得创建各类基于Spring的企业级应用变得异常简单。Node.js作为一种基于ChromeV8引擎的JavaScript运行时环境,在服务端上运行JavaScript代码。它以其独......
  • VSCode 配置Markdown模板
    ⇦1.工具篇-VSCode快捷键的使用待新增⇨前言避免重复写博客的相同部分,自定义markdown模板一.教程步骤总结1.配置settings.json快捷键ctrl+shift+P打开命令面板输入settings.json选中openusersettings添加配置:"[markdown]":{"editor.quickSuggestions":......
  • (转)对比学习:Golang VS Python3
    原文:https://juejin.cn/post/6844903843050815502Golang和Python都是目前在各自领域最流行的开发语言之一。Golang其高效而又友好的语法,赢得了很多后端开发人员的青睐,最适用于高并发网络编程的语言之一。Python不用说,TIOBE排行榜的前十常驻居民,现在已经稳定在前五了。在机器......
  • vscode调整界面大小,缩放字体快捷键
    1、打开设置菜单2、输入‘FontLigatures’,选择编辑js文件3、增加代码"editor.mouseWheelZoom":true  ......
  • VSS忘记Admin密码和修改默认登陆用户
    1.忘记Visualsourcesafe管理员密码打开vss数据库所在的文件夹,打开data目录,找到um.dat文件,用编辑器打开编辑它,从00000080h开始将值改为如下文所述的样子,然后保存,这样um.dat文件就回到了初始状态,然后打开vssadmin,用admin用户登录,就不需要密码了。0:805555bc7f41646d......
  • 大屏自适应/适配方案【详解】(echarts自适配、rem、flexible.js、vscode中px2rem插件自
    原文链接:https://blog.csdn.net/weixin_41192489/article/details/120004315大屏自适应的核心目标有:字体大小随屏幕尺寸自适应变化详见下文自动撑满全屏设置如下.bg{background:url("~@/assets/images/主背景.png");position:fixed;top:0;left:0;wi......
  • 对称加密 vs 非对称加密
    计算机网络在给我们带来便利的同时,也存在很多安全隐患,比如信息伪造,病毒入侵,端点监听,SQL注入等,给我们日常生活造成很严重的影响。那么这篇文章我就跟大家聊聊常见的网络安全隐患,只作为科普,不能作为网安系列文章。网络安全性威胁的种类在网络通信中可能会受到各种各样的潜在的安......
  • Fetch API res.buffer vs res.arrayBuffer All In One
    FetchAPIres.buffervsres.arrayBufferAllInOneerrorTypeError[ERR_INVALID_ARG_TYPE]:The"data"argumentmustbeoftypestringoraninstanceofBuffer,TypedArray,orDataView.ReceivedaninstanceofArrayBufferimportfsfrom'n......
  • vscode 终端报错:无法加载文件 D:\nodejs\npm.ps1,因为在此系统上禁止运行脚本。
    问题:vscode终端执行npm指令时报错如下解决:1.在终端通过输入指令get-ExecutionPolicy得到以下结果:表示当前执行策略为受限的2.执行指令Set-ExecutionPolicy-ScopeCurrentUser,并输入RemoteSigned。Set-ExecutionPolicy语法如下:Set-ExecutionPolicy[-Execu......
  • 文心一言 VS 讯飞星火 VS chatgpt (91)-- 算法导论8.3 4题
    四、用go语言,说明如何在O(n)时间内,对0到$n^3-1$区间内的n个整数进行排序。文心一言:在O(n)时间内对0到n^3-1区间内的n个整数进行排序,可以使用基数排序(RadixSort)算法。基数排序是一种非比较型整数排序算法,其时间复杂度为O(d*(n+k)),其中d是数字的最大位数,k是基......