Call method after some delay in java

前端 未结 2 2042
鱼传尺愫
鱼传尺愫 2021-02-20 00:44

Scenario is like :

In my application, I opened one file, updated it and saved. Once the file saved event get fired and it will execute one method abc(). But

2条回答
  •  迷失自我
    2021-02-20 01:37

    Use Timer and TimerTask

    create a member variable of type Timer in YourClassType

    lets say: private Timer timer = new Timer();

    and your method will look something like this:

    public synchronized void abcCaller() {
        this.timer.cancel(); //this will cancel the current task. if there is no active task, nothing happens
        this.timer = new Timer();
    
        TimerTask action = new TimerTask() {
            public void run() {
                YourClassType.abc(); //as you said in the comments: abc is a static method
            }
    
        };
    
        this.timer.schedule(action, 60000); //this starts the task
    }
    

提交回复
热议问题