0 votes
in Java/J2EE by
What will happen when you attempt to compile and run the following code?


public class WaNot{
int i=0;
public static void main(String argv[]){
        WaNot w = new WaNot();
        w.amethod();
        }
        public void amethod(){
	while(true){
		try{ 
        		wait();
		    }catch (InterruptedException e) {}
		i++;
	    }//End of while

	}//End of amethod
}//End of class
Choices:
Compile time error, no matching notify within the method (0 votes)
Compile and run but an infinite looping of the while method (0 votes)
Compilation and run (0 votes)
Runtime Exception "IllegalMonitorStatException" (2 votes)
related to an answer for: Why do you need the wait/notify protocol?
   

2 Answers

0 votes
by Hot Users (380 points)
4) Runtime Exception "IllegalMonitorStateException"

The wait/notify protocol can only be used within code that is synchronized. In this case calling code does not have a lock on the object and will thus cause an Exception at runtime.
0 votes
by Hot Users (380 points)

There is only two solutions for the above code to run having only wait method.

1. Make the method synchronized

public void synchronized amethod(){

while(true){
try{
        wait();
    }catch (InterruptedException e) {}
i++;
    }//End of while

}//End of amethod
 

2. Put wait inside a synchronized block having lock to the current object (this)

public void amethod(){

synchronized (this) {
while(true){
try{
        wait();
    }catch (InterruptedException e) {}
i++;
    }//End of while

}

}//End of amethod

 

Note: If we write synchronized block on locking the class, it will again thorw the same error. The below also will throw the error.

public void amethod(){

synchronized (WaNot.class) {
while(true){
try{
        wait();
    }catch (InterruptedException e) {}
i++;
    }//End of while

}

}//End of amethod
 

 

 

 

Related questions

0 votes
1 answer 130 views
0 votes
1 answer 137 views
0 votes
1 answer 130 views
Welcome to Find4Answers.com

Where you can Ask Questions, Find Answers Or Receive Answers from other members of the community And Share in Social networking sites like facebook, linkedin, twitter.
3cx phone system bandar bola benches boiler business bóg chwilówka przez internet car car lease deals car leasing uk chwilówka przez internet zapewne company convey convey law complaints convey law reviews convey law service conveylaw cornwall cosmetic dentist data data recovery maidenhead data recovery nottingham deals deepika padukone deepika padukone hot design development dot net programming dzieje szybkie chwilówki electrician electrician manchester electricians emergency farm filmy bez limitu filmy bez limitów filmy online finlock finlock solutions general genral graduate jobs in london graduate jobs london gutters hard drive recovery heap http: www.seo-assist.in infrared sauna infrared sauna saunas inline skating installation jabong coupon java judi bola laptop recycling law law firm in leeds lease leasing led lights led bulbs leeds life insurance quotes log london manchester movies museums and art myntra coupon nikogo szybka chwilówka outsource link building personal porcelain veneers ramię szybkie chwilówki recovery seo seo agencies seo company seo company london seo services services solutions stair lifts steel synchronization szybka chwilówka przykład szybka chwilówka żaden szybkie chwilówki lekko thread tunnel lighting tymczasem chwilówki przez internet upvc windows manchester viagra kamagra videos vinyl flooring suppliers wait web design services london web development company window repairs london windows yebhi coupon zobaczyć szybka chwilówka
...