Home > Back-end >  Java process communication problems
Java process communication problems

Time:04-30

 package ThreadTest; 


import java.util.ArrayList;
import java.util.List;

Public class FirstThread {
The static List Goods=new ArrayList<> (a);//store items warehouse, store at most 1
Public static void main (String [] args) throws InterruptedException {
Producer thread1=new Producer ();
Consumer thread2=new Consumer ();

Thread p1=new Thread (thread1, "producer 1");
Thread the p2=new Thread (thread1, "producer 2");
Thread p3=new Thread (thread1, "producer 3");
Thread c1=new Thread (thread2, "consumer 1");
Thread the c2=new Thread (thread2, "consumer 2");
Thread c3=new Thread (thread2, "consumers");

P1. The start ();
P2. Start ();
P3. Start ();
C1. Start ();
C2. Start ();
C3. The start ();

}
Static class Producer implements Runnable {

@ Override
Public void the run () {
//TODO Auto - generated method stub
Int num=0;
While (true) {
Synchronized (goods) {
If (goods. The size ()==0) {
Goods. The add (" goods "+ + + num);
System. The out. Println (Thread. CurrentThread (). The getName () + "produced the first" + num + "products");
}
Else if (goods. The size () & gt; 0)
Try {
Goods. Wait ();
{} catch InterruptedException (e)
//TODO Auto - generated the catch block,
e.printStackTrace();
}
}
Try {
Thread.sleep(100);
{} catch InterruptedException (e)
//TODO Auto - generated the catch block,
e.printStackTrace();
}
}
}
}
Static class Consumer implements Runnable {

@ Override
Public void the run () {
//TODO Auto - generated method stub
Int num=0;
While (true) {
Synchronized (goods) {
If (goods. The size () & gt; 0 {
Goods. Remove (" goods "+ + + num);
System. The out. Println (Thread. CurrentThread (). The getName () + "consumer first" + num + "products");
}
Else if (goods. The size ()==0)
Goods. Notify ();;
}
Try {
Thread.sleep(100);
{} catch InterruptedException (e)
//TODO Auto - generated the catch block,
e.printStackTrace();
}
}
}

}
}

Why do all the producers are not Shared a num, every producer output the output of the 1, 2... , I according to the example of the book can be Shared, if there is a problem of the inner class, but if there is no inner class I and how to realize the communication between producers and consumers,
Ask a question, by the way, that is I according to the example of the book to knock, but every time the result of the execution is almost completed by a window all ticketing, wasn't up to the window of the upper and lower two tickets must be different, the code is as follows:
Morally sleep (100) is not the same thread execute continuous
 package ThreadTest; 

The class SaleThread2 implements Runnable {
Int tickets=10;
Object lock=new Object();
@ Override
Public void the run () {
//TODO Auto - generated method stub
While (true) {
Synchronized (lock) {
If (tickets> 0 {
Try {
Thread.sleep(100);
{} catch InterruptedException (e)
//TODO Auto - generated the catch block,
e.printStackTrace();
}
System. The out. Println (Thread. CurrentThread (). The getName () + "is selling the first" + tickets - + "ticket");
}
}
}
}

}

Public class Example12 {
Public static void main (String [] args) {
SaleThread2 st=new SaleThread2 ();
New Thread (st, "window 1"). The start ();
New Thread (st, "window 2"). The start ();
New Thread (st, "window 3"). The start ();
New Thread (st, 4 "window"). The start ();
}
}
  • Related