Java multi-threading (6) - two kinds of producers and consumers to achieve

A tube odometry

Use a buffer, producers and consumers are based on the number of activities in the buffer to
Here Insert Picture Description
set the buffer has three benefits:

  1. Concurrent threads to achieve cooperation
    in the future with the buffer, the producer thread only need to be placed inside the buffer data, without the need to manage the situation of consumer spending; likewise, consumers only need to take data from the buffer can handle, not require producers to produce the pipe case.
    Thus, to achieve a separation of "producer thread" and "consumer threads" from the logic.
  2. Decoupling of producer and consumer
    producers and consumers do not need to deal directly.
  3. Resolve busy uneven, improve efficiency
    when producers slow production data, there are data buffer, does not affect consumer spending; consumers slow data processing, producers can continue to place the data inside the buffer zone
public class Thread01 {
 public static void main(String[] agrs) {
  Container container=new Container();
  Productor p=new Productor(container);
  consumer c=new consumer(container);
  p.start();
  c.start();
 }
}
//生产者
class Productor extends Thread{
 Container con;
 public Productor(Container con) {
  this.con=con;
 }
 public void run() {
  //生产
  for(int i=1;i<=100;i++) {
   System.out.println("生产第:"+i+"个馒头");
   con.push(new Steamedbun(i));
  }
 }
}
//消费者
class consumer extends Thread{
 Container con;
 public consumer(Container con) {
  this.con=con;
 }
 public void run() {
  //消费
  for(int i=1;i<=100;i++) {
   {
    System.out.println("消费第:"+con.pop().id+"个馒头");
   }
  }
 }
}
//缓冲区
class Container{
 Steamedbun[] buns=new Steamedbun[10];//存储容器
 int count=0;
 //存储 生产
 public synchronized void push(Steamedbun bun) {
  //存在空间 能生产
  if(count==buns.length) {
   try {
    this.wait();
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
  buns[count]=bun;
  count++;
  this.notifyAll();
 }
 //取
 public synchronized Steamedbun pop() {
  //何时能消费-->容器中是否有数据
  if(count==0) {
   try {
    this.wait(); //生产者通知消费
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
  count--;
  Steamedbun bun=buns[count];
  this.notifyAll(); 
  return bun;
 }
}
class Steamedbun{
 int id;
 public Steamedbun(int id) {
  this.id=id;
 }
}

Second, the lights Act

Using the flag to determine the current state of completion of the activity, similar to traffic lights

public class Thread02 {
 public static void main(String[] agrs) {
  Tv tv=new Tv();
  new Player(tv).start();
  new Watcher(tv).start();
 }
}
//生产者 演员
class Player extends Thread{
 Tv tv;
 public Player(Tv tv) {
  this.tv=tv;
 }
 public void run() {
  for(int i=0;i<20;i++) {
   if(i%2==0) {
    this.tv.play("奇葩说");
   }else {
    this.tv.play("太污了,喝瓶立白洗洗嘴");
   }
  }
 }
}
//消费者 观众
class Watcher extends Thread{
 Tv tv;
 public Watcher(Tv tv) {
  this.tv=tv;
 }
 public void run() {
  for(int i=0;i<20;i++) {
   tv.watch();
  }
 }
}
//同一个资源 电视
class Tv{
 String voice;
 //如果为真 演员表演 观众等待
 //如果为假 演员等待 观众观看
 boolean flag=true;
 
 //表演
 public synchronized void play(String voice) {
  //演员等待
  if(!flag) {
   try {
    this.wait();
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
  //表演时刻
  System.out.println("表演了"+voice);
  this.voice=voice;
  this.notifyAll();
  this.flag=!this.flag;
 }
 //观看
 public synchronized void watch() {
  //观众等待
  if(flag) {
   try {
    this.wait();
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
  System.out.println("听到"+voice);
  this.notifyAll();
  this.flag=!this.flag;
 }
}
Published 45 original articles · won praise 43 · views 7067

Guess you like

Origin blog.csdn.net/qq_42193790/article/details/104441226