简体   繁体   English

在Java中以不同的间隔运行多个Timer任务

[英]Run multiple Timer tasks with different intervals in Java

Say I have the following code 说我有以下代码

 Timer t1 = new Timer();
 t1.scheduleAtFixedRate(new TimerTask() {
        @Override
         public void run(){
             //TASK 1
         }
 },0,2000);

 Timer t2 = new Timer();
 t2.scheduleAtFixedRate(new TimerTask() {
        @Override
         public void run(){
             //TASK 2
         }
 },0,180000);

Would Task1 and Task2 run independent of each other or would Task2 hold up the process for 30 minutes? Task1和Task2是彼此独立运行还是将Task2保留30分钟?

If the latter happens, would splitting this into two Threads be the only option? 如果发生后者,将其拆分为两个线程是否是唯一的选择?

From the Javadoc of Timer : TimerJavadoc中

Corresponding to each Timer object is a single background thread that is used to execute all of the timer's tasks, sequentially. 每个 Timer对象相对应的是一个单独的后台线程,该线程用于依次执行所有Timer的任务。

Each of your Timer instances will execute independently. 您的每个Timer实例将独立执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM