[英]How to make thread run something every x seconds? (Java)
I have a program, in there I have this thread which will recieve Strings from another user and save them in another object. 我有一个程序,在那里我有这个线程,它将从另一个用户接收字符串并将它们保存在另一个对象中。 I want to make this thread run the "seek for Strings and save them-method" every x seconds. 我希望每隔x秒使这个线程运行“寻找字符串并保存它们 - 方法”。 How do I do that? 我怎么做?
I believe this answer would be helpful to you: https://stackoverflow.com/a/12908477/3328790 我相信这个答案会对您有所帮助: https : //stackoverflow.com/a/12908477/3328790
The user suggests you use Timer and TimerTask classes which you can use to schedule your task to run every n seconds. 用户建议您使用Timer和TimerTask类,您可以使用它们来安排任务每n秒运行一次。
Create a Runnable
which would be used in your periodically running Thread
and create another Runnable
which will start the needed Thread
in conform of your needs. 创建一个Runnable
,它将在您定期运行的Thread
使用,并创建另一个Runnable
,它将根据您的需要启动所需的Thread
。 Start the second Thread
. 启动第二个Thread
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.