Java multithreading solves producer consumer problems
- 2020-04-01 03:52:33
- OfStack
This article illustrates how Java multithreading can solve producer-consumer problems. Share with you for your reference. Specific analysis is as follows:
The question is:
Using Java multithreading technology, design and implement a program in line with producer and consumer problems. To operate on an object (the chamber of a gun), the maximum capacity is 12 bullets. The producer thread is a push thread that keeps pushing bullets into the gun chamber. The consumer thread is an ejection thread that continuously fires bullets from the gun chamber.
Requirements:
(1) give a description of the analysis process.
(2) program output, to simulate the injection and injection of the gun chamber operation;
(2) the synchronization of two threads should be considered when designing the program.
This is almost the same as the famous producer-consumer problem, and here's a quick analysis.
Let's just say it in code, the comments are clear
package test;
import java.util.ArrayList;
import java.util.List;
public class testGun {
public static void main(String[] args)
{
GunClip clip=new GunClip();
Producer p=new Producer(clip);
Concumer c=new Concumer(clip);
p.start();
c.start();
}
}
class GunClip //cartridge
{
private List<Integer> list=null;//For firing bullets
private boolean bFull =false ; //It is important to understand the Boolean variable to indicate whether it is full or not
//The function functions like putting a bullet in a magazine
public synchronized void put(List list){
if(!bFull){//It means that the magazine is not full, so it's like putting a bullet in it
this.list=list;
bFull=true;//Full of bullets. Changing the Boolean variable to true means that the cartridge is full
notify();//Notifies the thread waiting for the bullet to retrieve the bullet
}
//The wait in the next try is when the magazine is full,
//Boolean true, suspends the thread and waits for the bullet
try{
wait();
}catch(Exception e){
e.printStackTrace();
}
}
public synchronized void get(){
if(!bFull){//If the magazine isn't full then you can't take the bullets
try{
wait();//The magazine is not full, so can not take, can only wait, only the magazine is full to take
}catch(Exception e){
e.printStackTrace();
}
}
//The bottom is full, so I'm going to get the bullets
System.out.println("n"+" The machine gun began to fire :"+"n");
for(int i=list.size();i>0;i--){
int j=(Integer)list.get(i-1);
System.out.println(" Already sent out the first "+j+" bullet ");
}
//The magazine is empty, meaning there are no bullets left, so you have to wait for it to be refilled,
//So Boolean becomes false to indicate it is not full, notifying the loaded thread to load
bFull=false;
notify();
}
}
//The producer, that is, the cartridge, is going to have a cartridge body down here, similar to the bulletin board
class Producer extends Thread
{
private GunClip clip;
private List<Integer> list;
Producer(GunClip clip){
this.clip=clip;
}
public void run(){
for(int i=0;i<3;i++){ //Let it produce three magazines
list=new ArrayList();
System.out.println("n"+" The machine gun began to load: "+"n");
for(int j=0;j<12;j++){
list.add(j+1);
System.out.println(" Has been pressed into the first "+(j+1)+" bullet ");
}
clip.put(list);
}
}
}
//Consumer, fire the bullet
class Concumer extends Thread
{
private GunClip clip;
Concumer(GunClip clip){
this.clip=clip;
}
public void run(){
while(true){
clip.get();
}
}
}
I hope this article has been helpful to your Java programming.