my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Important, about threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Important, about threads" Watch "Important, about threads" New topic
Author

Important, about threads

Kishan Kumar
Ranch Hand

Joined: Sep 26, 2000
Posts: 130
Hi all,
Till now i was learning threads conceptually. When i tried to
program the producer consumer example error at runtime is
occuring. see the code below

output :
true
true
produced 10
Got 10
IllegalMonitorStateException, thread not owner at
notify call in class consumer.
I could not find out the reason of this error. Please folks
though it is a large program anyone atleast try out and point
me the error. I can copy the program from book but i want to
know what error I am doing.
Thanks in Advacne for your time.

------------------
Regards,
V. Kishan Kumar


Regards,<BR>V. Kishan Kumar
Jon Aryan
Greenhorn

Joined: Oct 06, 2000
Posts: 22
Hi Kishan,
check ur code
--------------------
class consumer implements Runnable {
producer ab;
public consumer(producer pr) {
ab = pr;
}
public void consume(producer b) {
// ACQUIRE THE LOCK ON the Producer instance
synchronized(b) {
try {
while(!b.flag) {
// !!! WAIT on the "Consumer's" wait pool
wait();
// U can't do this !! rite ??, since u did not acquire the lock
// for this Consumer instance
// Instead u can say
// " b.wait() ", ie wait on "Producer" wait pool
// or synchronize "this", ie on "Consumer" instance
}
b.flag = false;
System.out.println("Got " + b.a);
// Same here !! u did not acquire the lock for this instance !!
// so u cannot notify the threads waiting on this instance monior
// !!!
notify();
/// !!!
Thread.sleep(1000);
.........
------------
[This message has been edited by Jon Aryan (edited October 12, 2000).]
Kishan Kumar
Ranch Hand

Joined: Sep 26, 2000
Posts: 130
Aryan,
Million thanks for going through my code. I went through your
interpretation and it was not a mistake because the lock was
not acquired but the culprit was the notify statement which
implicitly means this.notify but there are no threads wating
for this notify call from the consumer object.
I changed that statement to b.notify in the consumer thread
and ofcourse i forgot to put the for loop in the consumer thread.
Now the code works and i am definitely greatful to you for
spending your time on this. I am giving the changed code , see
if that works there.

again thank you very much.

------------------
Regards,
V. Kishan Kumar
Jon Aryan
Greenhorn

Joined: Oct 06, 2000
Posts: 22
Hi Kishan,
I thought the Java API has cheated me, when u told the above code is working !!!
-----------
public void consume(producer b) {
for(int i = 0;i<20;i++) {
synchronized(b) {
try {
while(!b.flag) {
System.out.println("waiting");
/////// Can't be !!! U did not acquire the lock for Consumer object
this.wait();
}
b.flag = false;
System.out.println("Got " + b.a);
b.notify();
Thread.sleep(1000);
} catch(InterruptedException e) {
System.out.println("Interrupted eception caught in consumer");
}
--------------
Did u noticed that the message in the while loop is not executing ???
--------------
Modify that code by removing the while loop, then execute the code !!! i mean like this
//while(!b.flag) {
this.wait();
//}

-------------
Then put the original while loop back, and comment the Thread.sleep() method from both the classes !!
-------------
I modifed ur code, try this out ..
------------
import java.util.*;
class producer implements Runnable{
public int a;
public boolean flag = false;
public synchronized void produce() {
for(int index = 0; index < 5; index++) {
try {
while(flag == true) {
System.out.println("Producer waiting ..");
wait();
}
a = (new Random()).nextInt();
flag = true;
System.out.println("produced " + a);
notifyAll();
} catch (InterruptedException e) {
System.out.println("Interrupted eception caught in producer");
}
}
}
public void run() {
produce();
}
}
class consumer implements Runnable {
producer ab;
public consumer(producer pr) {
ab = pr;
}
public void consume(producer b) {

for(int i = 0;i<5;i++) {
synchronized(b) {
try {
while(b.flag == false) {
System.out.println("Consumer waiting ...");
b.wait();
}
b.flag = false;
System.out.println("Got " + b.a);
b.notifyAll();
} catch(InterruptedException e) {
System.out.println("Interrupted eception caught in consumer");
}
}
}
}
public void run() {
consume(ab);
}
}
class prodcons {
public static void main(String args[]) {
producer p;
Thread tp = new Thread((p = new producer()));
Thread tc = new Thread(new consumer(p));
tc.setPriority(Thread.MAX_PRIORITY);
tp.setPriority(Thread.MIN_PRIORITY);
tp.start();
tc.start();
}
}
-----------

[This message has been edited by Jon Aryan (edited October 12, 2000).]
[This message has been edited by Jon Aryan (edited October 12, 2000).]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Important, about threads
 
Similar Threads
Problem with Thread Interaction
Thread question
Newbie Producer Consumer Problem
Thread wait() and notify()
wait( ) and notify( )