Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save dmpatel151282/b9019e07ddfc69fa9eb067ee71c9634d to your computer and use it in GitHub Desktop.

Select an option

Save dmpatel151282/b9019e07ddfc69fa9eb067ee71c9634d to your computer and use it in GitHub Desktop.
There are three ways to do this.
1.
private static final ScheduledExecutorService worker =
Executors.newSingleThreadScheduledExecutor();
void someMethod() {
...
Runnable task = new Runnable() {
public void run() {
/* Do something... */
}
};
worker.schedule(task, 5, TimeUnit.SECONDS);
...
}
2.
new Timer().schedule(new TimerTask() {
@Override
public void run() {
// This code will be executed after 2 seconds
}
}, 2000);
3.
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
//Do something after 100ms
}
}, 100);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment