Home > Back-end >  Would you please tell me why multithreaded added the synchronize output still wrong
Would you please tell me why multithreaded added the synchronize output still wrong

Time:10-22

Public class Test1 {
Public static void main (String [] args) {



AppleBox ab=new AppleBox ();

Producer p=new Producer (ab);
Producer p2=new Producer (ab);
Consumer c=new Consumer (ab);

Thread t=new Thread (p);
T.s etName (" producers a * * * * * ");
T.s tart ();

Thread t2=new Thread (p2);
T2. Elegantly-named setName (" producer & amp; & & & & amp;" );
T2. Start ();

The Thread t3=new Thread (c);
T3. Elegantly-named setName (" spending a # # # # # ");
T3. The start ();
}
}

The class Producer implements Runnable {
AppleBox AppleBox;

Public Producer (AppleBox AppleBox) {
Enclosing appleBox=appleBox;
}

@ Override
Public void the run () {
//production to appleBox 10 apples,
For (int I=0; I & lt; 5; I++) {
Apple is a=new Apple (I);
AppleBox. Deposite (a);

Try {
Thread.sleep ((int) (Math. The random () * 1000));
{} catch InterruptedException (e)
e.printStackTrace();
}
}
}
}

The class Consumer implements Runnable {
AppleBox AppleBox;

Public Consumer (AppleBox AppleBox) {
Enclosing appleBox=appleBox;
}

@ Override
Public void the run () {
For (int I=0; I & lt; 10; I++) {
Apple is a=appleBox. Withdraw ();
Try {
Thread.sleep ((int) (Math. The random () * 1000));
{} catch InterruptedException (e)
e.printStackTrace();
}
}
}
}

The class AppleBox {
Int index=0;
Apple [] apples=new Apple [5];

Public synchronized void deposite (Apple Apple) {
//determine whether apples is full, full, is can't save
While (index & gt;=apples. Length) {
Try {
This. Wait ();
{} catch InterruptedException (e)
e.printStackTrace();
}
}

Apples [index]=apple;
index++;
System. The out. Println (Thread. CurrentThread (). The getName () + "produced" + apple);
//not full, just save
Enclosing notifyAll ();
}

Public synchronized Apple withdraw () {
While (index==0) {
Try {
This. Wait ();
{} catch InterruptedException (e)
e.printStackTrace();
}

}

Apple is a=apples [index - 1);
The index -;
Enclosing notifyAll ();
System. The out. Println (Thread. CurrentThread (). The getName () + "consumption" + a);
return a;
}

}

The class Apple {
Int id;

Apple (int id) {
this.id=id;
}

Public String toString () {
Return "apple" + id;
}
}



Output:
Producers a * * * * * production apple 0
Consumption a # # # # # the apple 0
Producers 2 & amp; & & & & The production of apple 0
Consumption a # # # # # the apple 0
Producers 2 & amp; & & & & Production of the apple 1
Producers a * * * * * production apple 1
Producers 2 & amp; & & & & Production of the apple 2
Consumption a # # # # # the apple 2
Consumption a # # # # # the apple 1
Producers 2 & amp; & & & & Production of apple 3
Producers of a * * * * * made the apple 2
Producers a * * * * * production apple 3
Consumption a # # # # # the apple 3
Producers a * * * * * production apple 4
Producers 2 & amp; & & & & The production of apple 4
Consumption a # # # # # the apple 4
Consumption a # # # # # the apple 4
Consumption a # # # # # the apple 2
Consumption a # # # # # the apple 3
Consumption a # # # # # the apple 1

The output line 5 6 rows produced apple1
  • Related