public class sleep1 extends Thread {
public sleep1(String s) {
super(s);
}
@Override
public void run() {
for (int i = 1; i <= 4; i++) {
if (i % 2 == 0) {
try {
Thread.sleep(1000);
System.out.println("Thread is sleeping "
+ Thread.currentThread());
} catch (Exception e) {
}
}
else {
System.out.println("Thread is not sleeping "
+ Thread.currentThread());
}
}
}
public static void main(String[] args) {
sleep1 s1=new sleep1("mythread1");
sleep1 s2=new sleep1("mythread2");
s1.start();
s2.start();
}
}
Output
Thread is not sleeping Thread[mythread1,5,main] Thread is not sleeping Thread[mythread2,5,main] Thread is sleeping Thread[mythread2,5,main] Thread is sleeping Thread[mythread1,5,main] Thread is not sleeping Thread[mythread1,5,main] Thread is not sleeping Thread[mythread2,5,main] Thread is sleeping Thread[mythread1,5,main] Thread is sleeping Thread[mythread2,5,main]If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.