Pbroblems with Synchronized methods --- please help

** One Thread calls a synchronized method using an object (obj1) of a Class (Class name - meth_class) and occupies the monitor of that particular object (obj1). Then no other Thread can call that particular synchronized method or any other synchronized method of the object (obj1) of a Class (Class name - meth_class). **
          IF THE ABOVE DEFINITION IS TRUE PLEASE HELP ME OUT WITH THE FOLLOWING:
Please go through the code below:-
class meth_class
          volatile boolean valueset=false;
          int var;
          synchronized int get()
                    if (!valueset)
                              try
                                        wait();
                              catch (InterruptedException e)
                                        System.out.println("Consumer interrupted");
                    System.out.println("Get - " + var);
                    valueset=false;
                    notify();
                    return var;
          synchronized void put(int x)
                    if (valueset)
                              try
                                        wait();
                              catch (InterruptedException e)
                                        System.out.println("Producer interrupted");
                    this.var=x;
                    System.out.println("Put - " + var);
                    valueset=true;
                    notify();
class producer implements Runnable
          meth_class obj;
          Thread thrd;
          producer(meth_class x)
                    obj=x;
                    thrd=new Thread(this, "Producer");
                    thrd.start();
          public void run()
                    int var=0;
                    while(true)
                              obj.put(var++);
class consumer implements Runnable
          meth_class obj;
          Thread thrd;
          consumer(meth_class x)
                    obj=x;
                    thrd=new Thread(this, "Consumer");
                    thrd.start();
          public void run()
                    while(true)
                              obj.get();
class main_thrd
          public static void main(String args[])
                    meth_class obj=new meth_class();
                    new producer(obj);
                    new consumer(obj);
                    System.out.println("Press Control-C to stop");
My Questions are:
1.     class main_thrd creates two objects as ?new producer(obj)? and ?new consumer(obj)?. Now both these objects creates 2 threads and try to call two different Synchronized methods of same CLASS ? ?meth_class? through a single object by the name of ?obj? created in class main_thrd. Now as ?new producer(obj)? object is created first in the class main_thrd, the Thread created by it enters the monitor of object (obj) and locks it. Then as per the rule the Thread created by ?new consumer(obj)? object never gets to occupy the monitor of the object (obj) because Producer is in an infinite loop. BUT THEN WHY WE HAVE ? GET - ** ? VALUES PRINTED IN THE OUTPUT.
Please any one help me out,
(THANKS FOR ALL YOUR HELP)

Now as ?new producer(obj)? object is called first in the class main_thrd, the Thread created by it enters the monitor of object (obj) and locks it.Though new producer(obj) is called first, it is not necessary that the thread created by it gets the lock of object(obj) first. This is false
assumption.
Next, even if we assumed that it gets the lock of given object first,
while(true)
    obj.put(var++);
}The lock of Object(obj) is released when the obj.put(var++) returns and any one can get hold of lock of the Object(obj) at that instant.
In fact, next time obj.put(var++) is called, it will have to wait until it can get lock of the given object.

Similar Messages

Maybe you are looking for

  • PC Suite 6.86.9.0 Backup Failure

    PC Suite 6.86.9.0 Backup Failure When backing up the messages on my N80(firmware v5.0719.0.2) the backup always fails when processing multimedia messages and windows reports "Windows Driver Foundation - User-mode Driver Framework Host Process" (WUDFH

  • Help Required regding: Validation on Data Loading from Flat File

    Hi Experts, I need u r help in the following issue. I need to validated the transactional data loading to the GL Cube from Flat file, 1) The transactional data to the Cube to be loaded <b>only if master data</b> record exists for the <b>“0GL_ACCOUNT”

  • Can you use Drobo as external disk on WRT610N?

    I looked around on the forum here for info, but couldn't find anything.  I am attempting to hook up a Drobo to my WRT610N and it doesn't show up under the router setup page.  Am I doing something wrong or is this just not a supported device of this r

  • Can't get coorrect txt report

    hello I tried to generate TXT report using Report Builder as "generate to file..." but my output was incorrect. I tried to use all of templates. my TXT file looks like this : col_name1 col_name2 .. col_namei valOfcol1 valOfcol2 ... col_name1 col_name

  • What program to open downloaded PSE 10

    Trying to open download of PSE10, asking for program to open, what is it/