Home > Back-end >  Why this error happened!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Why this error happened!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Time:04-15

Public class TestOne {

Public static void main (String [] args) {
The Container Container=new Container ();

New Producier (container). The start ();
New Customer (container). The start ();
}
}

//producer
The class Producier extends Thread {
The Container Container;
Public Producier Container (Container) {
This. The container=container;
}

@ Override
Public void the run () {
For (int I=0; I & lt; 100; I++) {
Container. Push (new Chicken (I));
System. Out.println (" production - & gt;" + I + "chickens");
}
}
}

//consumer
The class Customer extends Thread {
The Container Container;
The public Customer (Container Container) {
This. The container=container;
}

@ Override
Public void the run () {
For (int I=0; I & lt; 100; I++) {
System. Out. Println (" removed "+ container. Pop (). The id +" chickens ");
}
}
}

//commodity id
The class Chicken {
//commodity id
int id;
Public Chicken (int id) {
this.id=id;
}
}

//buffer
Class Container {
//declare the container size
Chickens=new Chicken Chicken [] [10].
//the number of container
Int count=0;

Put things
//producerPublic synchronized void push (Chicken Chicken) {
If (count==10) {
//to inform consumers'
Try {
This. Wait ();
{} catch InterruptedException (e)
e.printStackTrace();
}
}
//to the warehouse to add things
Chickens (count)=chicken;
count++;

//to inform consumers'
Enclosing notifyAll ();
}

//consumers take
Public synchronized Chicken pop () {
If (count==0) {
//wait for producers are
Try {
This. Wait ();
{} catch InterruptedException (e)
e.printStackTrace();
}
}

//consumption
Chicken Chicken=chickens (count);
The count -;

//notification producers are
Enclosing notifyAll ();
Return the chicken;
}

}



The Exception in the thread "thread - 1" Java. Lang. ArrayIndexOutOfBoundsException: 10
The at Day05. Container. Pop (TestOne. Java: 97)
The at Day05. Customer. The run (TestOne. Java: 46)
  • Related