简体   繁体   中英

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. How do I do that?

I believe this answer would be helpful to you: 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.

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. Start the second Thread .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM