• Post Reply Bookmark Topic Watch Topic
  • New Topic

notify() &notifyAll()

 
Jitendra Jha
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having a lot of difficulty in using notify and notifyAll methods.Whenever i try to do so i get IllegalMonitorStateException:current class not owner.
Please help me out.Please explain what this means and where am i doing wrong and what is the correct way to do it.All the books on this issue seem to confuse me.Here is my code.all the places where i have tried notify has been commented.they were just guess works!!


class threads_a extends base2all implements Runnable
{

Thread t;

public threads_a(String str)
{
t=new Thread(this,str);
t.start();
//notifyAll();

}

synchronized public void run()
{
try
{
for(int i=0;i<8;i++)
{
System.out.println("The Current Thread is : "+t.currentThread()+"

"+i);
t.sleep(200);
if(!a)
{
wait();
}
}
notifyAll();

}
catch(InterruptedException e)
{
System.out.println("Caught");
}
}

}

class threads_b extends base2all implements Runnable
{

Thread t;

public threads_b(String str)
{
t=new Thread(this,str);
t.start();
}

synchronized public void run()
{
try
{
for(int i=0;i<8;i++)
{
System.out.println("The Current Thread is : "+t.currentThread()+"

"+i);
t.sleep(400);
//t.notifyAll();
}

}
catch(InterruptedException e)
{
System.out.println("Caught");
}
}
}

class threads_c extends base2all implements Runnable
{

Thread t;
threads_a tta=new threads_a("First");
threads_b ttb=new threads_b("Second");

public threads_c(String str)
{
t=new Thread(this,str);
t.start();
}



synchronized public void run()
{
try
{
for(int i=0;i<8;i++)
{
System.out.println("The Current Thread is : "+t.currentThread()+"

"+i);
t.sleep(800);
if(i==6)
{
a=true;
//tta.t.notifyAll();
}

}
}
catch(InterruptedException e)
{
System.out.println("Caught");
}

}
}



public class threads4 extends base2all
{

public static void main(String... args) throws

InterruptedException,IllegalMonitorStateException
{

threads_c tc=new threads_c("Third");
tc.t.join();
System.out.println(a);
System.out.println(tc.tta.t.isAlive());
System.out.println(tc.ttb.t.isAlive());
System.out.println(tc.t.isAlive());

}
}
 
Remko Strating
Ranch Hand
Posts: 893
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tried to run your code, but I get a lot of different errors.

The message is IllegalMonitorStateException is given when you're calling the notify method on a thread which is doesn't own the lock.

Reading your code I don't understand what kind of locks you're using. The synchronized run code doesn't own locks on objects.

Try to use notify() within code which has a lock on a object
 
Jitendra Jha
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There lies the problem.actually my concept about these is not very clear.I fail to understand these terms.Technically i know about these but practically i don't.please help with these(wait(),notify(),notifyAll()) methods.i mean what does owning a lock mean and how do i know who is owning it
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By "owning the lock on an object", he means being inside a synchronized method of that object, or a block synchronized on that object. So, for example, this code is legal:

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!