Question 7
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[]){
Threat t1=new A(sa);t1.start();
synchronized(sa){
sa[0]="Done";
sa[1]="A";sa[2]="B";sa[3]="C";
sa.notify();
}}}
Answer is : ABC .True but not right
Thread t1 is started and will move into the Running state at the discretion of the thread scheduler.
The A.run metod invokes the wait method on the String array object sa
"only if we change sa[0]="Done" to any different value,in that case will be an infinity loop in wait(),because sa.notify() dont't get the lock of sa back(doesnt't interrupt the waiting loop).
When I replaced sa.notify() whith t1.interrupt() then the synchronized block run to completation.
Please can I get a good explanation