+ 2

How do i implement thread.sleep in this code snippet

Plis help https://code.sololearn.com/cVPMitZ6mCrg/?ref=app

21st Mar 2019, 2:55 PM
stephen haokip
stephen haokip - avatar
2 Answers
+ 7
You can instead use Handler. Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { //Do something after 1sec } }, 1000);
21st Mar 2019, 3:00 PM
Letsintegreat
Letsintegreat - avatar
+ 6
class DrawImage implements Runnable { public void run() { for(int i = 0; i < 10; i++) { try { System.out.println("Drawing image..."); Thread.sleep(100); } catch (InterruptedException ex) { System.out.println("Thread interupted"); } } } } class PlayMusic implements Runnable { public void run() { for(int i = 0; i < 10; i++) { try { System.out.println("Playing music..."); Thread.sleep(150); } catch (InterruptedException ex) { System.out.println("Thread interupted"); } } } } class CreatingThreadWithRunnable{ public static void main(String[ ] args) { Thread t1 = new Thread(new DrawImage()); t1.start(); Thread t2 = new Thread(new PlayMusic()); t2.start(); } } You can use Thread.sleep(TIME_IN_MILLI_SECS) function inside try and catch.
21st Mar 2019, 9:04 PM
blACk sh4d0w
blACk sh4d0w - avatar