How do you utilize timers in Java? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How do you utilize timers in Java?

How does one create a java timer which functions similar to the setInterval() feature in javascript? I would to have example() called every 10 seconds until a while loop condition is satisfied in the calling method. Any suggestions?

30th Jan 2017, 5:27 PM
Daniel
Daniel - avatar
3 Answers
+ 1
After researching the timer object, this is the answer I've come up with: import java.util.Timer; import java.util.TimerTask; public class Example{ public static void main(String[]args){ Timer timer = new Timer(); timer.scheduleAtFixedRate(new TimerTask(){ public void run(){ // Execute example code here } }, 0, 10000); } }
31st Jan 2017, 5:07 AM
Daniel
Daniel - avatar
0
Timer t = new Timer(); I forget the parameters.. Google Java Timer()
30th Jan 2017, 8:52 PM
LordHill
LordHill - avatar
0
if it doesn't work, it might need a thread
31st Jan 2017, 12:13 PM
LordHill
LordHill - avatar