ScheduledExecutorService定时器



import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class TestDamo {
    
    public static void main(String[] args){
        //ScheduledExecutorService 内部为线程池 弥补了Timer的缺陷
        ScheduledExecutorService pool = Executors.newScheduledThreadPool(3);
        pool.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread()+"执行一次");
            }
        },0,2, TimeUnit.SECONDS);
    }
}
ScheduledExecutorService 内部为线程池 弥补了Timer的缺陷
    原文作者:洋啊桑815
    原文地址: https://blog.csdn.net/weixin_66344432/article/details/125632157
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞