Printing even odd can be achieved without wait and notify, want understand is wait notify gives better performance over below code ?
public class EvenOdd {
public static void main(String[] args) {
Thread t1= new ThreadToPrint("Even");
Thread t2= new ThreadToPrint("Odd");
t1.start();
t2.start();
}
}
class ThreadToPrint extends Thread{
static Integer count = 0;
static int MAX = 100;
static boolean printEven = true;
public ThreadToPrint(String string) {
super(string);
}
@Override
public void run() {
while (count < MAX) {
if ("Even".equals(Thread.currentThread().getName()) && printEven) {
System.out.println("*************"+Thread.currentThread().getName() + " Printing :" + count++);
printEven=false;
} else if ("Odd".equals(Thread.currentThread().getName()) && !printEven) {
System.out.println(Thread.currentThread().getName() + " Printing :" + count++);
printEven=true;
}
}
}
}