IllegalMonitorStateException: current thread not owner

Discussion in 'Java' started by Riaz Uddin Ahmed, Aug 10, 2003.

  1. I've given the source code and the corresponding error generated by
    JVM. Idea behind the program is there will be few Threads running
    which will check a global data area whether has any data or not. If no
    data is there, one of the Threads will put data and will notify all.
    Otherwise the Thread will wait. Except these Thread, there will be
    another thread which will check has data in global area or not. The
    Thread will wait if has no data otherwise it will do something. I
    tried my best to solve this situation. Please check my code & error
    report and let me know

    Thanks in advance.....

    <<Code>>
    public class Sender extends Thread
    {
    static String data = "";
    static int machineId;

    public static void main(String[] args)
    {
    SenderChild sc1 = new SenderChild(1);
    Thread s1 = new Thread(sc1);
    s1.start();
    Sender sender = new Sender();
    sender.start();
    }
    public void run()
    {
    while(true)
    {
    if( Sender.hasData() )
    {
    Sender.putData("");
    notifyAll();
    }
    else
    {
    try
    {wait(); }
    catch(InterruptedException ie){}
    }
    }
    }
    static synchronized boolean hasData()
    {
    if(data == null || data.length() == 0)
    return false;
    return true;
    }
    static synchronized void putData(String data)
    {
    Sender.data = data;
    }
    static synchronized void machineNo(int id)
    {
    Sender.machineId = id;
    }
    }
    class SenderChild implements Runnable
    {
    int i;
    int index = 0;
    public SenderChild(int id)
    {
    i = id;
    }
    public void run()
    {
    while(true)
    {
    System.out.println(""+i);
    if(Sender.hasData())
    {
    try{wait();}
    catch(InterruptedException ie)
    {
    }
    }
    else
    {
    Sender.putData("From machine no:- "+i+" for "+index+" times");
    Sender.machineNo(i);
    notifyAll();
    }}}}




    <<Error report>>
    java.lang.IllegalMonitorStateException: current thread not owner
    at java.lang.Object.notifyAll(Native Method)
    at SenderChild.run(Sender.java:101)
    at java.lang.Thread.run(Thread.java:536)
    java.lang.IllegalMonitorStateException: current thread not owner
    at java.lang.Object.notifyAll(Native Method)
    at Sender.run(Sender.java:35)
    Riaz Uddin Ahmed, Aug 10, 2003
    #1
    1. Advertising

  2. You need to be synchronized on the object on which you're calling wait()
    or notify().

    You are not calling notify() on the same object on which you're calling
    wait().
    David Zimmerman, Aug 10, 2003
    #2
    1. Advertising

  3. "Riaz Uddin Ahmed" <> wrote in message
    news:...
    > I've given the source code and the corresponding error generated by
    > JVM. Idea behind the program is there will be few Threads running
    > which will check a global data area whether has any data or not. If no
    > data is there, one of the Threads will put data and will notify all.
    > Otherwise the Thread will wait. Except these Thread, there will be
    > another thread which will check has data in global area or not. The
    > Thread will wait if has no data otherwise it will do something. I
    > tried my best to solve this situation. Please check my code & error
    > report and let me know


    Look at the java.lang.Object documentation - find the notifyAll
    method. Read why notifyAll may throw an IllegalMonitorStateException.



    >
    > Thanks in advance.....
    >
    > <<Code>>
    > public class Sender extends Thread
    > {
    > static String data = "";
    > static int machineId;
    >
    > public static void main(String[] args)
    > {
    > SenderChild sc1 = new SenderChild(1);
    > Thread s1 = new Thread(sc1);
    > s1.start();
    > Sender sender = new Sender();
    > sender.start();
    > }
    > public void run()
    > {
    > while(true)
    > {
    > if( Sender.hasData() )
    > {
    > Sender.putData("");
    > notifyAll();
    > }
    > else
    > {
    > try
    > {wait(); }
    > catch(InterruptedException ie){}
    > }
    > }
    > }
    > static synchronized boolean hasData()
    > {
    > if(data == null || data.length() == 0)
    > return false;
    > return true;
    > }
    > static synchronized void putData(String data)
    > {
    > Sender.data = data;
    > }
    > static synchronized void machineNo(int id)
    > {
    > Sender.machineId = id;
    > }
    > }
    > class SenderChild implements Runnable
    > {
    > int i;
    > int index = 0;
    > public SenderChild(int id)
    > {
    > i = id;
    > }
    > public void run()
    > {
    > while(true)
    > {
    > System.out.println(""+i);
    > if(Sender.hasData())
    > {
    > try{wait();}
    > catch(InterruptedException ie)
    > {
    > }
    > }
    > else
    > {
    > Sender.putData("From machine no:- "+i+" for "+index+" times");
    > Sender.machineNo(i);
    > notifyAll();
    > }}}}
    >
    >
    >
    >
    > <<Error report>>
    > java.lang.IllegalMonitorStateException: current thread not owner
    > at java.lang.Object.notifyAll(Native Method)
    > at SenderChild.run(Sender.java:101)
    > at java.lang.Thread.run(Thread.java:536)
    > java.lang.IllegalMonitorStateException: current thread not owner
    > at java.lang.Object.notifyAll(Native Method)
    > at Sender.run(Sender.java:35)
    William Brogden, Aug 10, 2003
    #3
  4. Riaz Uddin Ahmed

    Roedy Green Guest

    Roedy Green, Aug 14, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Madhur Ahuja
    Replies:
    4
    Views:
    22,849
    juliadavi22
    Aug 11, 2007
  2. morizn

    illegalMonitorStateException

    morizn, Feb 4, 2005, in forum: Java
    Replies:
    9
    Views:
    7,511
    Chris Smith
    Feb 6, 2005
  3. laredotornado
    Replies:
    3
    Views:
    3,412
  4. sharan
    Replies:
    0
    Views:
    588
    sharan
    Dec 2, 2009
  5. Replies:
    3
    Views:
    169
Loading...

Share This Page