传统定时器技术 发表于 2017-07-16 | 更新于 2023-02-20 | 分类于 编程语言 , Java | 阅读次数 传统定时器技术5秒后爆炸,然后每隔3秒爆炸一次123456789101112131415161718192021@Testpublic void testTimer01() { /** * 面向对象的方式思考:一个定时器实例,拥有计划schedule方法,具体的任务由TimerTask实现. */ new Timer().schedule(new TimerTask() { @Override public void run() { System.out.println(Thread.currentThread().getName() + " !!!bombing!!! "); } }, 5000, 3000); while (true) { System.out.println(Thread.currentThread().getName() + " " + Calendar.getInstance().get(Calendar.SECOND)); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } }} 结果12345678910111213141516main 27main 28main 29main 30main 31Timer-0 !!!bombing!!! main 32main 33main 34Timer-0 !!!bombing!!! main 35main 36main 37Timer-0 !!!bombing!!! main 38main 39 赏 微信打赏