What is the equivalent of javascript setTimeout in Java?

前端 未结 9 875
梦毁少年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:50

    There is setTimeout() method in underscore-java library.

    Code example:

    import com.github.underscore.U;
    import com.github.underscore.Function;
    
    public class Main {
    
        public static void main(String[] args) {
            final Integer[] counter = new Integer[] {0};
            Function incr = new Function() { public Void apply() {
                counter[0]++; return null; } };
            U.setTimeout(incr, 100);
        }
    }
    

    The function will be started in 100ms with a new thread.

提交回复
热议问题