aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why it is giving errors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why it is giving errors" Watch "why it is giving errors" New topic
Author

why it is giving errors

Raji Addepalli
Ranch Hand

Joined: Dec 05, 2001
Posts: 50
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

Joined: Dec 20, 2001
Posts: 3271
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


SCJP Tipline, etc.
sonir shah
Ranch Hand

Joined: Nov 01, 2001
Posts: 435
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

Joined: Jan 10, 2002
Posts: 7
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

Joined: Dec 05, 2001
Posts: 50
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

Joined: Mar 22, 2000
Posts: 12823
    
    5
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

Joined: Jan 08, 2002
Posts: 54
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.


help you means help me
 
Consider Paul's rocket mass heater.
 
subject: why it is giving errors