I need to implement a function to run after 60 seconds of clicking a button. Please help, I used the Timer class, but I think that that is not the best way.
相关问题
- Delete Messages from a Topic in Apache Kafka
- Is there a limit to how many levels you can nest i
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- How to toggle on Order in ReactJS
its run after 60 seconds from scheduleAtFixedRate https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ScheduledExecutorService.html
You should use
Thread.sleep()
method.This will wait for 60,000 milliseconds(60 seconds) and then execute the next statements in your code.
Use Java 9 CompletableFuture, every simple:
The other answers assume you are not using Swing for your user interface (button).
If you are using Swing then do not use
Thread.sleep()
as it will freeze your Swing application.Instead you should use a
javax.swing.Timer
.See the Java tutorial How to Use Swing Timers and Lesson: Concurrency in Swing for more information and examples.
Using the java.util.Timer:
Here you can find some info and examples.
There is
setTimeout()
method in underscore-java library.Code example:
The function will be started in 100ms with a new thread.