• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why it is giving errors

 
Raji Addepalli
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Wait1 {
Buffer buffer[] = new Buffer[10];
public class Producer extends Thread {

public void run()
{
int i=0;
for(i=0;i<25;++i)
{
{
if(buffer[i % 100].full)
try { buffer[i].wait();
//wait if buffer if full
}
catch(Exception ex) {}
buffer[i % 100].i =
(int)(100 * Math.random());
System.out.println("Filling buffer "+i
+" with "+buffer[i % 10].i);
buffer[i % 10].full = true;
buffer[i % 10].notify();
try{sleep((int)(100*Math.random()));
} catch(Exception ex) {}
}
}
}
}
public class Consumer extends Thread {
public void run()
{
int i=0;
for(i=0;i<25;++i)
{
synchronized(buffer[i % 10]) {
if(!buffer[i % 10].full)
try { buffer[i].wait();
//wait if buffer if empty
}
catch(Exception ex) {}
System.out.println("Buffer "+i
+" is : "+buffer[i % 10].i);
buffer[i % 10].full = false;
buffer[i % 10].notify();
try{ sleep((int)(1000*Math.random()));
}catch(Exception ex){}
}
}
}
}
class buffer {
int i;
boolean full = false;
}
public static void main(String args[])
{
Wait1 w=new Wait1();
for(int i = 0;i<w.buffer.length;++i)
w.buffer[i]=w.new buffer();
Producer p=w.new Producer();
Consumer c=w.new Consumer();
c.start(); p.start();
}
}
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What kind of errors are you getting? Are they compiler errors? If so, what are they? Are they run-time errors? If so, what are you getting and what were you expecting? Also, please use code tags around your code so that it is easier to read. It's difficult enough to look through that much code. Try to help us by pointing us in the right direction.
Corey
 
sonir shah
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what a HUGE code??
Do we get such kind of codes in the SCJP exam, Raji??
To understand such a code is really a big thing
Sonir
 
shivaji
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The line that declares class Buffer needs to change from
class buffer {
to
class Buffer (
Also, in the main method of the wait class the call to new needs to be
new Buffer, not new buffer.
HTH
 
Raji Addepalli
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shivaji thanks .you are correct.
after the run :I got it as filling buffer 0 with 54
java.lang.IllegalMonitorStateException:current thread not owner at java.lang.Object.notify(Native Method)
at Wait$Producer.run(Wait1.java:25).
Now tell me what to do?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13055
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error message is very descriptive. You can't call notify() on an object unless the code is synchronized on that object.
In other words - the thread is not the owner of the monitor so it can't call notify()
Bill
 
Seany Iris
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by William Brogden:
The error message is very descriptive. You can't call notify() on an object unless the code is synchronized on that object.
In other words - the thread is not the owner of the monitor so it can't call notify()
Bill

Thank you very much!
Can I understand what you said like this:
Only after you call the method synchronized(),you can call notify().
Waiting for your answer,thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic