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 |
how to one war file class to another war file class?
Define "Access specifiers" in java.
whst is encapsulation?when u encpsulate actually while devoloping code?
What is the Scope of Static Variable?
Why we should declare the variables as static and final in interfaces?
Explain different ways of creating a thread. Which one would you prefer and why?
I have a string like _a01_a02_a03_ and another string like _2_1.5_4_ as input.I want to extract a01,a02... to a string array and 2,1.5,etc to a double array with a01 corresponds to 2 and a02 to 1.5 etc. Need code in core java.. Can you do it?
Difference between the paint() and repaint() methods?
What is the basic functionality of DataOutput interface in java?
Does java have extension methods?
What is anonymous inner class?
what is the main difference between string and stringbuffer? can you explain it with program?