What is the equivalent of javascript setTimeout in Java?

前端 未结 9 877
梦毁少年i
梦毁少年i 2020-12-13 06:23

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.

9条回答
  •  时光取名叫无心
    2020-12-13 06:35

    You can simply use Thread.sleep() for this purpose. But if you are working in a multithreaded environment with a user interface, you would want to perform this in the separate thread to avoid the sleep to block the user interface.

    try{
        Thread.sleep(60000);
        // Then do something meaningful...
    }catch(InterruptedException e){
        e.printStackTrace();
    }
    

提交回复
热议问题