• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Important, about threads

 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
See where your hand is? Not there. It's next to this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic