I use synchronized
in order to execute first thread and then execute another when the first thread finished, but both threads are executed at the same time. Why?
public class PrintNums extends Thread {
int num;
public PrintNums(int x) {
this.num = x;
}
@Override
public void run() {
this.count();
}
public synchronized void count() {
for (int i = 1; i <= 5; i++) {
System.out.println((2 * i - this.num));
try {
Thread.currentThread().sleep(1000);
} catch (InterruptedException ex) {
}
}
}
public static void main(String[] args) {
PrintNums odd = new PrintNums(1);
PrintNums even = new PrintNums(0);
odd.start();
even.start();
}
}