write a code,
we have two thread, one is printing even no and other print the odd no.
public class EvenOdd {
public static void main(String[] args) {
final Printer printer = new Printer();
new Thread(new Runnable() {
@Override
public void run() {
int i = 1;
while (i < 100) {
printer.printOdd(i);
i = i + 2;
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
int i = 2;
while (i < 100) {
printer.printEven(i);
i = i + 2;
}
}
}).start();
}
static class Printer {
boolean isOdd = true;
synchronized public void printOdd(int number) {
while (!isOdd) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.print(number + " ");
isOdd = false;
notify();
}
synchronized public void printEven(int number) {
while (isOdd) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.print(number + " ");
isOdd = true;
notify();
}
}
}
Is This Answer Correct ? | 0 Yes | 0 No |
Explain about map interface in java?
Hi Friends.. can any one provide the real time example for methodoverloading and methodoverriding .........
How do you invoke a method?
What is thread safe java?
What is the function of character?
What is the difference between a public and a non-public class?
What is the purpose of void?
What is meant by binding in rmi?
A abstract class extending an abstract class.Super class has both abstract and non-abstract methods.How can we implement abstract and non-abstract mehtods? Explain with snippet
What are actual parameters?
What is the main functionality of Prepared Statement?
Explain about automatic type conversion in java?