Last active
September 9, 2016 04:34
-
-
Save dmpatel151282/b9019e07ddfc69fa9eb067ee71c9634d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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