+ 2
How do i implement thread.sleep in this code snippet
2 ответов
+ 7
You can instead use Handler.
Handler handler = new Handler(); handler.postDelayed(new Runnable() {
@Override public void run() {
//Do something after 1sec
}
}, 1000);
+ 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.