multithreading - Call method after some delay in java -


scenario :

in application, opened 1 file, updated , saved. once file saved event fired , execute 1 method abc(). now, want add delay after save event fired, 1 minute. have added thread.sleep(60000). execute method abc() after 1 minute. till works fine.

but suppose user saved file 3 times within 1 minute, method executed 3 times after each 1 minute. want execute method 1 time in next 1 minute after first save called latest file content.

how can handle such scenario?

use timer , timertask

create member variable of type timer in yourclasstype

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

and method this:

public synchronized void abccaller() {     this.timer.cancel(); //this cancel current task. if there no active task, nothing happens     this.timer = new timer();      timertask action = new timertask() {         public void run() {             yourclasstype.abc(); //as said in comments: abc static method         }      };      this.timer.schedule(action, 60000); //this starts task } 

Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -