Create repeated timer reminders in Java

I want a class that changes its private variables every 2 seconds I know if I do something

import java.util.Timer;
//...
Timer timer;
//...
timer.schedule(new ChangeSomething(),2000);

It will execute changesomething () in 2 seconds. Is there any way to tell it to do something every 2 seconds, or if I put it in changesomething()

timer.schedule(new ChangeSomething(),2000);

Will it work

In the marginal note, timer What exactly did cancel() do?

Solution

Use timer Scheduleatfixedrate() to schedule it to repeat every two seconds:

From for timer Javadoc of cancel():

Edit:

About the internal execution thread of the timer that executes a task once:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>