class A extends
Thread {
String[] sa;
public A(String[] sa) {this.sa = sa;}
public void run() {
synchronized (sa) {
while (!sa[0].equals("Done")) {
try {sa.wait();} catch (InterruptedException ie) {}
}}
System.out.print(sa[1] + sa[2] + sa[3]);
}}
class B {
private static String[] sa = new String[]{"Not Done","X","Y","Z"};
public static void main (String[] args) {
Thread t1 = new A(sa); t1.start();
synchronized (sa) {
sa[0] = "Done"; //1
sa[1] = "A"; sa[2] = "B"; sa[3] = "C";
sa.notify();
}}}
The given output for this program is ABC.The explanation is that thread t1 enters wait state and by the time notify gets called from main method all values are changed.
But by the time t1 gets to exeute run() method the main thread may execute line 1.Thus with sa[0]= "Done" t1 may not enter into wait state at all.Is there no such possibility?If no, why?