我有这个问题。
如果可能的话,我需要无休止地重复一个方法,但不会使系统超载。
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:以指定的时间间隔执行方法
发布评论