Java:以指定的时间间隔执行方法

编程入门 行业动态 更新时间:2024-10-27 12:35:58
本文介绍了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:以指定的时间间隔执行方法

本文发布于:2023-10-16 11:28:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1497426.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:间隔   时间   方法   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!