I am supposed to create 3 threads first will print 'A'
every 3 seconds, second 'B'
every 4 seconds and 'C'
every 5 seconds.
I did it like that but they change their order durign printing and it is suspicous. But I think that in 9th second they can mix... and its fine. Please post yuur opinion.
public class MyThread extends Thread {
static int ID;
int id;
public MyThread() {
id = ID++;
}
public synchronized void run() {
char character = (char) (65 + id);
while (true) {
System.out.println(character);
try {
Thread.sleep(3000 + id * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
new MyThread().start();
new MyThread().start();
new MyThread().start();
}
}
Output:
A
B
C
A
B
C
A
B
A
C
B
A
.......