如何在JAVA中每隔一段时间执行一段程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
可以用线程来做,每隔几秒开一个线程
代码如下
    public void runTask() {
        final long timeInterval = 120000;// 两分钟运行一次
        final ThreadService threadService = new ThreadService();
        Runnable runnable = new Runnable() {
            public void run() {
                while (true) {
                    // ------- code for task to run
//你要运行的程序
                    // ------- ends here
                    try {
                        Thread.sleep(timeInterval);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        Thread thread = new Thread(runnable);
        thread.start();
    }

 

posted @   锐洋智能  阅读(1799)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· 分享4款.NET开源、免费、实用的商城系统
· 解决跨域问题的这6种方案,真香!
· 5. Nginx 负载均衡配置案例(附有详细截图说明++)
· Windows 提权-UAC 绕过
历史上的今天:
2017-05-09 Java从网络批量获取图片并保存至本网站服务器后再自动插入文章中
2017-05-09 Java Split以竖线作为分隔符
2014-05-09 CentOS查看CPU、内存、网络流量和磁盘 I/O
2014-05-09 CentOS 6.5安装配置Nginx
点击右上角即可分享
微信分享提示