File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes notify() &notifyAll() Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "notify() &notifyAll()" Watch "notify() &notifyAll()" New topic
Author

notify() &notifyAll()

Jitendra Jha
Ranch Hand

Joined: Jan 28, 2007
Posts: 91
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());

}
}


Jitendra
SCJP1.5
SCWCD1.5
Remko Strating
Ranch Hand

Joined: Dec 28, 2006
Posts: 893
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


Remko (My website)
SCJP 1.5, SCWCD 1.4, SCDJWS 1.4, SCBCD 1.5, ITIL(Manager), Prince2(Practitioner), Reading/ gaining experience for SCEA,
Jitendra Jha
Ranch Hand

Joined: Jan 28, 2007
Posts: 91
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
Marshal

Joined: Jul 08, 2003
Posts: 24081
    
  15

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:



[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: notify() &notifyAll()
 
Similar Threads
wait() not syncronized on this but not throwing illegalMonitorStateException
Synchronized
wait and notifyAll
Threads
wait()/notify()