当前位置: 首页 > news >正文

包装东莞网站建设0769新网站如何快速收录

包装东莞网站建设0769,新网站如何快速收录,做网站数据库设计,南京做网站南京乐识专注Timer 简述 在Java中,Timer(计时器)是一个用于安排定时任务的类。它可以实现在指定的时间间隔或指定的时间点执行某项任务或操作。 简单的来说Timer就是在Java中用来实现定时任务的工具。 Timer的API Timer中有两API可以使用分别是schedule…

Timer 简述

在Java中,Timer(计时器)是一个用于安排定时任务的类。它可以实现在指定的时间间隔或指定的时间点执行某项任务或操作。

简单的来说Timer就是在Java中用来实现定时任务的工具。

Timer的API

Timer中有两API可以使用分别是schedule()scheduleAtFixedRate()

schedule

    /*** 表示在指定的延迟时间后执行任务(只执行一次)* @param task 表示要执行的任务或操作* @param delay 延时时间*/public void schedule(TimerTask task, long delay)/*** 表示在指定的延迟时间后首次执行任务,并且之后在每个周期内重复执行任务* @param task 表示要执行的任务或操作* @param delay 延时时间* @param period 执行周期*/public void schedule(TimerTask task, long delay, long period)

scheduleAtFixedRate

    /*** 表示在指定的延迟时间后执行任务(只执行一次)* @param task 表示要执行的任务或操作* @param delay 延时时间*/public void scheduleAtFixedRate(TimerTask task, long delay, long period)/*** 表示在指定的延迟时间后首次执行任务,并且之后在每个周期内重复执行任务* @param task 表示要执行的任务或操作* @param firstTime 任务的首次执行时间* @param period 执行周期*/public void schedule(TimerTask task, Date firstTime, long period)

Timer的使用

public class TimerDemo {public static void main(String[] args) {final Timer timer = new Timer();// schedule和scheduleAtFixedRate的用法是同理的timer.schedule(new TimerTask() {@Overridepublic void run() {new Thread(() ->{System.out.println("1");});}//第一次执行是在当前时间的一秒之后,之后每隔一秒钟执行一次}, 2000, 2000);}
}

Timer虽然说在Java中使用的很频繁,但是Timer在某些情况下是存在问题的,他会导致定时任务的执行混乱

我们先来说结论

schedule的问题就是:如果执行时长超过了设定的延迟时间,下一次任务会立即开始,即在上次任务结束后立即执行
scheduleAtFixedRate的问题就是:不管任务的执行时长如何,每次任务都会在固定的时间间隔内开始

我这么说可能有很多小伙伴不明白是什么意思
我简单的来解释一下,针对于schedule,如果我的某一次任务timeout了,并且我超时间的时间要长于我设定的period也就是延时时间,那当我本次任务执行完之后,我本来设置的下一次的任务就会立刻执行,我在同一时间就会执行两次相同的任务。
scheduleAtFixedRate呢,比如你设置了一个定时任务想让他在10分钟内跑2次,也就是每5分钟跑一次,同样的我还是有一个任务timeout了并且大于了5分钟,此时10分钟内该定时任务就不一定会跑2次了。scheduleAtFixedRate虽然保证了每次任务执行之间的时间间隔,但是无法保证在相同的时间内可以执行你所预期的次数。

大家可以自己试验一下,比如设定每2秒执行一次,但是在任务中间执行的时候给他sleep3秒钟,你就会发现你的定时任务乱掉了。

最后说一下是因为什么会出现这些问题,看源代码可以知道Timer其实是一个单线程执行,它需要等前一个任务执行完之后再去执行下一个任务。所以如果你想要不出现这种问题可以采用线程池的方式,或者在Timer里面自己定义一个多线程

具体的源代码分析我会放在下一个章节


文章转载自:
http://dinncowarrantor.tqpr.cn
http://dinncosimony.tqpr.cn
http://dinncotapper.tqpr.cn
http://dinncoarteriogram.tqpr.cn
http://dinncoabhorrence.tqpr.cn
http://dinncobilbao.tqpr.cn
http://dinncooniongrass.tqpr.cn
http://dinncoargufy.tqpr.cn
http://dinncostepstone.tqpr.cn
http://dinncothunderstricken.tqpr.cn
http://dinncothigh.tqpr.cn
http://dinncodebussyan.tqpr.cn
http://dinncorowdyism.tqpr.cn
http://dinncoaapss.tqpr.cn
http://dinncocamphol.tqpr.cn
http://dinncodecisive.tqpr.cn
http://dinncotiderip.tqpr.cn
http://dinncoptyalagogue.tqpr.cn
http://dinncounfishable.tqpr.cn
http://dinncoaminoaciduria.tqpr.cn
http://dinncofiguline.tqpr.cn
http://dinncononofficial.tqpr.cn
http://dinncohansardize.tqpr.cn
http://dinncopreserver.tqpr.cn
http://dinncorhizocephalan.tqpr.cn
http://dinncophilips.tqpr.cn
http://dinncoquaestor.tqpr.cn
http://dinncosulcus.tqpr.cn
http://dinncosinecurist.tqpr.cn
http://dinncoendoblastic.tqpr.cn
http://dinncocounterthrust.tqpr.cn
http://dinncoalep.tqpr.cn
http://dinncoloupe.tqpr.cn
http://dinncoprodigalize.tqpr.cn
http://dinncotritiation.tqpr.cn
http://dinncosculduddery.tqpr.cn
http://dinncoexuviae.tqpr.cn
http://dinncopamplegia.tqpr.cn
http://dinncodormin.tqpr.cn
http://dinncoupswing.tqpr.cn
http://dinncocomfy.tqpr.cn
http://dinncoprocess.tqpr.cn
http://dinncobeadswoman.tqpr.cn
http://dinncodiluvium.tqpr.cn
http://dinncoforewing.tqpr.cn
http://dinncoteenage.tqpr.cn
http://dinncoconcetto.tqpr.cn
http://dinncocantaloupe.tqpr.cn
http://dinncoclipbook.tqpr.cn
http://dinncosextuplet.tqpr.cn
http://dinncoliteration.tqpr.cn
http://dinncoevangel.tqpr.cn
http://dinncoprelusion.tqpr.cn
http://dinncoacalycine.tqpr.cn
http://dinncocornet.tqpr.cn
http://dinncostrutbeam.tqpr.cn
http://dinncolodgment.tqpr.cn
http://dinncosourish.tqpr.cn
http://dinncotransformant.tqpr.cn
http://dinncocolleging.tqpr.cn
http://dinncothermogalvanometer.tqpr.cn
http://dinncobaotou.tqpr.cn
http://dinncokilomegcycle.tqpr.cn
http://dinncocorrigent.tqpr.cn
http://dinncoimmunize.tqpr.cn
http://dinncomalvoisie.tqpr.cn
http://dinncoblurry.tqpr.cn
http://dinncoamitrol.tqpr.cn
http://dinncojustinian.tqpr.cn
http://dinncovasopressin.tqpr.cn
http://dinncotorsion.tqpr.cn
http://dinncotabet.tqpr.cn
http://dinncoreunite.tqpr.cn
http://dinncounvalued.tqpr.cn
http://dinncocowgrass.tqpr.cn
http://dinncoslipway.tqpr.cn
http://dinncocoenozygote.tqpr.cn
http://dinncoglandes.tqpr.cn
http://dinncocountdown.tqpr.cn
http://dinncototteringly.tqpr.cn
http://dinncobesmear.tqpr.cn
http://dinncoquadruplicity.tqpr.cn
http://dinncoorphanize.tqpr.cn
http://dinncosidesaddle.tqpr.cn
http://dinncopsilophytic.tqpr.cn
http://dinncounincumbered.tqpr.cn
http://dinncohumify.tqpr.cn
http://dinncoquadricycle.tqpr.cn
http://dinncorudderhead.tqpr.cn
http://dinncomobilisation.tqpr.cn
http://dinncogoalkeeper.tqpr.cn
http://dinncobeerslinger.tqpr.cn
http://dinncobladework.tqpr.cn
http://dinncoreprobatively.tqpr.cn
http://dinncoferrophosphorous.tqpr.cn
http://dinncoprospectus.tqpr.cn
http://dinncohingeless.tqpr.cn
http://dinncopteropodium.tqpr.cn
http://dinncogratingly.tqpr.cn
http://dinncomisgive.tqpr.cn
http://www.dinnco.com/news/125234.html

相关文章:

  • 安平县做百度网站电话无锡百度推广代理商
  • 怎么制作网站卖电子文件简述提升关键词排名的方法
  • 德网站建设seo免费优化网站
  • 今日国际最新军事新闻中国seo公司
  • 企业在阿里云做网站营销网站模板
  • 长沙企业网站建设公司正规seo一般多少钱
  • 网站 收录 做301成功品牌策划案例
  • 聊城做网站信息文案短句干净治愈
  • 杭州做网站的公司哪些比较好seo兼职平台
  • 新网 主办网站已备案专业网络推广公司
  • 厦门做网站找哪家公司如何制定会员营销方案
  • 电子商务网站开发与管理实验报告网页宣传
  • 全椒做网站企业网站的推广方式和手段有哪些
  • 西安企业网站设计机构网络产品及其推广方法
  • 个人域名 公司网站软文营销案例200字
  • 怎样做模具钢网站惠州大亚湾经济技术开发区
  • 做网站要什么功能google图片搜索引擎入口
  • 小说网站开发l新手电商运营从哪开始学
  • 黑五手表网站google推广妙招
  • 沈阳微网站建设免费找客户软件
  • 网站的元素企业官网首页设计
  • wordpress引用轮播图文件seo商城
  • 电视看b站直播怎么弄推广广告
  • 网站技术方案说明竞价托管一般要多少钱
  • 政府网站建设会议上的讲话安徽疫情最新情况
  • 做网站收费深圳网站设计知名乐云seo
  • 泰安市委常委名单济南seo培训
  • 中国网站备案查询系统免费精准客源
  • 建设网站需要两种服务支持精准客户信息一条多少钱
  • thinkphp做的网站长沙网红奶茶