Java:以指定的时间间隔执行方法 [英] Java: Execute a method at a specified time interval

查看:924
本文介绍了Java:以指定的时间间隔执行方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个问题。

如果可能的话,我需要无休止地重复一个方法,但不会使系统超载。

I need to have a method repeated endlessly, if possible, but without overloading the system.

据我所知,虽然(true){... code ...}会挂起系统。

As I understand, while(true) { ...code... } will hang the system.

您建议我使用什么?哪个不会挂起系统?

What would you suggest me to use which won't hang the system?

谢谢

另外:
执行我的方法可能需要5分钟,或50毫秒,我希望方法在完成时重复jsut

Also: The execution of a my method may take up to 5 minutes, or 50 milliseconds, and I want method to be repeated jsut when it is finished

推荐答案

你可以启动一个新线程并在后台运行任务。有关详细信息,请参阅 Java并发教程。

You could start a new Thread and run the task in the background. See the Java concurrency tutorial for more info.

如果您只想一次为此任务运行1个线程,则可以使用单线程执行器

If you only want to run 1 Thread for this task at a time, you could do it with a single thread executor.

这篇关于Java:以指定的时间间隔执行方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆