• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 447
  • Last Modified:

threads synchronized wait notify

>>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

1)Compile time error, no matching notify within the method
2)Compile and run but an infinite looping of the while method
3)Compilation and run
4)Runtime Exception "IllegalMonitorStatException"



Answer


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.








I read above question and answer from link
http://www.jchq.net/certkey/0703certkey.htm

It is not clear to me in terms how to specify synchronized with respect to wait and notify/notifyAll usage.

Any ideas, resources, links, sample code highly appreciated. thanks in advance.
0
gudii9
Asked:
gudii9
  • 7
  • 5
1 Solution
 
objectsCommented:
wait() and notify need to be called from a synchronized block
so the code would need to be changed to


public class WaNot {
      int i = 0;

      public static void main(String argv[]) {
            WaNot w = new WaNot();
            w.amethod();
      }

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

      }// End of amethod
}// End of class

Though as you have notify the result would then be


1)Compile time error, no matching notify within the method

0
 
gudii9Author Commented:
how do we mention notify to avoid Compile time error, no matching notify within the method. please advise
0
 
objectsCommented:
thats not a compile time error. It just means the code will be blocked at the wiat() ie it will never return
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
objectsCommented:
     public static void main(String argv[]) {
            WaNot w = new WaNot();
            w.amethod();
            synchronized (w) {
                w.notify();
            }
      }
0
 
gudii9Author Commented:
can i run these two classes together same timeon my eclipse IDE to see the functioning of notify in conjunction with wait. please advise
0
 
objectsCommented:
sure
0
 
gudii9Author Commented:
something like this

 
class Test{
      public static void main(String argv[]) {

            WaNot w = new WaNot();
            w.amethod();
            synchronized (w) {
                w.notify();
            }
      }
}





public class WaNot {
      int i = 0;

      public static void main(String argv[]) {
            WaNot w = new WaNot();
            w.amethod();
      }

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

      }// End of amethod
}// End of classs

please advise
0
 
gudii9Author Commented:
it is not running like this. please advise
0
 
objectsCommented:
you don't need a Test class WaNot already has a main, just run it
0
 
gudii9Author Commented:
you mean i need to run like


public class WaNot {
      int i = 0;

      public static void main(String argv[]) {
            WaNot w = new WaNot();
            System.out.println("hh");
            w.amethod();
            synchronized (w) {
                w.notify();
            }
      }

      public void amethod() {
            while (true) {
                System.out.println("hh2");
                  try {
                        synchronized (this) {
                            System.out.println("hh3");
                              wait();
                        }
                  } catch (InterruptedException e) {
                  }
                  i++;
            }// End of while

      }// End of amethod
}// End of class


please advise
0
 
gudii9Author Commented:
even i comment notify synchronized block code


public class WaNot {
      int i = 0;

      public static void main(String argv[]) {
            WaNot w = new WaNot();
            System.out.println("hh");
            w.amethod();
           /* synchronized (w) {
                w.notify();
            }*/
      }

      public void amethod() {
            while (true) {
                System.out.println("hh2");
                  try {
                        synchronized (this) {
                            System.out.println("hh3");
                              wait();
                        }
                  } catch (InterruptedException e) {
                  }
                  i++;
            }// End of while

      }// End of amethod
}// End of class


 it is still working fine. What is the importance of wait and notify. can you please elaborate on that
0
 
gudii9Author Commented:
even i comment notify synchronized block code


public class WaNot {
      int i = 0;

      public static void main(String argv[]) {
            WaNot w = new WaNot();
            System.out.println("hh");
            w.amethod();
           /* synchronized (w) {
                w.notify();
            }*/
      }

      public void amethod() {
            while (true) {
                System.out.println("hh2");
                  try {
                        synchronized (this) {
                            System.out.println("hh3");
                              wait();
                        }
                  } catch (InterruptedException e) {
                  }
                  i++;
            }// End of while

      }// End of amethod
}// End of class


 it is still working fine. What is the importance of wait and notify. can you please elaborate on that
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now