Analyze the following code.
// Test.java: Define threads using the Thread class
import java.util.*;
public class Test {
private Stack stack = new Stack();
private int i = 0;
/** Main method */
public static void main(String[] args) {
}
public Test() {
}
class Producer extends Thread {
| System.out.println("Producer: put " + i); |
| stack.push(new Integer(i++)); |
}
class Consumer extends Thread {
| System.out.println("Consumer: get " + stack.pop()); |
| catch (InterruptedException ex) { |
}
}
◦ The program has a compilation error on the notifyAll() method in the Producer class because it is not invoked from the stack object.
◦ The program will throw an exception because the notifyAll() method in the Producer class is not invoked from the stack object.
◦ The program has a logic error because the lock obtained by the synchronized block for notifyAll in the Producer class is stack and it should be this (i.e., synchronized (this) { notifyAll(); }).
◦ The program creates two threads: one to add data to the stack and the other to get data from the stack.