• Register
First time here? Checkout the FAQ!
x
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 assignment writing service bandar bola best waikiki restaurants boiler business car car insurance quotes car insurance quotes car insurance quotes car lease deals car leasing uk chwilówka przez internet zapewne company convey convey law complaints convey law reviews convey law service conveylaw cosmetic dentist csr classics hack csr classics hack csr classics hack data data recovery maidenhead deals design development double glazing leeds electrician electrician manchester electricians emergency farm filmy bez limitu filmy online finlock finlock solutions general genral golf holidays spain graduate jobs in london graduate jobs london heap how to jump higher how to jump higher how to jump higher infrared sauna infrared sauna saunas inline skating java judi bola law law firm in leeds lease leasing led lights led bulbs leeds legal smoke life insurance quotes log london manchester mercedes lease deals mercedes leasing mezzanine movies museums and art nikogo szybka chwilówka none none none ny male revue outsource link building paid search agency performance car hire personal personal injury lawyer porcelain veneers ramię szybkie chwilówki recovery restaurant seo seo agencies seo company seo company london seo las vegas seo services services sky diamond seo solutions steel synchronization szybka chwilówka przykład szybka chwilówka żaden szybkie chwilówki lekko temp cover car insurance temp cover car insurance temp cover car insurance thread tymczasem chwilówki przez internet upvc windows manchester viagra kamagra videos vinyl flooring suppliers wait web development company window repairs london windows zobaczyć szybka chwilówka

What will happen when you attempt to compile and run the following code? [poll]

0 votes
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?
asked Nov 15, 2011 in Java/J2EE by anonymous
    

2 Answers

0 votes
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.
answered Nov 15, 2011 by support Hot Users (380 points)
0 votes

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
 

 

 

 

answered Nov 16, 2011 by javaboy Hot Users (380 points)

Related questions

0 votes
1 answer 149 views
0 votes
1 answer 187 views
0 votes
1 answer 179 views
...