This was a question asked in one of my job interviews:
You have 2 different classes (that implements Runnable) say EvenThread & OddThread. As the name suggests, the EvenThread prints only even numbers and the odd thread prints only odd numbers, consider a range of 0-100.
class EvenThread implements Runnable {
@Override
public void run() {
for (int i = 0; i <= 10; i += 2) {
System.out.println(i);
}
}
}
class OddThread implements Runnable {
@Override
public void run() {
for (int i = 1; i < 10; i += 2) {
System.out.println(i);
}
}
}
public class EvenOdd {
public static void main(String args[]) {
Thread tEven = new Thread(new EvenThread());
Thread tOdd = new Thread(new OddThread());
tEven.start();
tOdd.start();
}
}
Now we need to enforce a mechanism in such a way that, the numbers are printed in sequence (i.e. 0, 1, 2, 3, 4,.... and so on).
I have seen many similar questions in Stack Overflow, but they have just one class to print number and 2 synchronized methods are invoked in it.
Could any of the experts please suggest?