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