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?
Asked
Active
Viewed 7,167 times
0
-
Have a look at TimerTask – Dhana Krishnasamy Feb 20 '14 at 15:39
-
http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ScheduledExecutorService.html – assylias Feb 20 '14 at 15:40
-
or http://docs.oracle.com/javase/7/docs/api/java/util/Timer.html#scheduleAtFixedRate%28java.util.TimerTask,%20long,%20long%29 – Pshemo Feb 20 '14 at 15:41
-
1Do as @assylias suggests: use a `ScheduledExecutorService`. It is _much_ better than a `Timer{,Task}` combination. – fge Feb 20 '14 at 15:41
2 Answers
2
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.
Community
- 1
- 1
LadyBytemod7 s
- 51
- 3
-
As commented above, using a `java.util.concurrent.ScheduledExecutorService` is a much better solution than a `Timer`/`TimerTask` arrangement. – dcsohl Feb 20 '14 at 16:53
0
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.
Lajos Arpad
- 64,414
- 37
- 100
- 175