多线程通信

多线程

实现runnable 继承Thread 线程池 callable

静态代理

真实对象和代理对象都要事先同一接口

代理对象需要代理真实对象

实际上多线程thread也是静态代码实现的

线程通信

解决线程同步方法

  • wait 线程等待,会释放索

  • notify 唤醒等待的线程

 

利用缓冲区 --管程法

public class TestPC {
   public static void main(String[] args) {
       SynContainer synContainer = new SynContainer();
       new Productor(synContainer).start();
       new Consumer(synContainer).start();
  }
}

class Productor extends Thread{
   SynContainer container;
   Productor(SynContainer container){
       this.container = container;
  }
   @Override
   public void run() {
       for (int i = 0; i < 100 ; i++) {
           System.out.println("生产了"+i+"产品");
           container.push(new Chicken(i));
      }
  }
}
class Consumer extends Thread{
   SynContainer container;
   Consumer(SynContainer container){
       this.container = container;
  }
   @Override
   public void run() {
       for (int i = 0; i < 100; i++) {
           Chicken c = container.pop();
           System.out.println("消费了"+c.id + "===");
      }
  }
}
// 产品
class Chicken{
   int id;
   public Chicken(int id) {
       this.id = id;
  }
}
class SynContainer{
   //容器大小
   Chicken[] chickens = new Chicken[10];
   //容器计数
   int count = 0;
   // 生产产品
   public synchronized void push(Chicken c){
       if(count == chickens.length){
           try {
               this.wait();// 线程等待
          } catch (InterruptedException e) {
               e.printStackTrace();
          }
      }
       chickens[count] = c;
       count++;
       this.notifyAll();// 通知消费者消费
  }
   // 消费者消费
   public synchronized Chicken pop(){
       if(count == 0){
           try {
               this.wait();
          } catch (InterruptedException e) {
               e.printStackTrace();
          }
      }
       count--;
       Chicken chicken = chickens[count];
       this.notifyAll();// 通知可以生产了
       return chicken;
  }
}

信号灯法

public class TestPC {
   public static void main(String[] args) {
       Chicken chicken = new Chicken();
       new Productor(chicken).start();
       new Consumer(chicken).start();
  }
}

class Productor extends Thread{
   Chicken chicken ;
   Productor(Chicken chicken){
       this.chicken = chicken;
  }
   @Override
   public void run() {
       for (int i = 0; i < 100 ; i++) {
           this.chicken.push(i);
           System.out.println("生产了"+i+"个产品");

      }
  }
}
class Consumer extends Thread{
   Chicken chicken ;
   Consumer(Chicken chicken){
       this.chicken = chicken;
  }
   @Override
   public void run() {
       for (int i = 0; i < 100; i++) {
          chicken.pop();

      }
  }
}
// 产品
class Chicken{
   int id;
   boolean flag = true;// 判断是否有产品
   public synchronized void push(int i){
       if(!flag){
           try {
               this.wait();
          } catch (InterruptedException e) {
               e.printStackTrace();
          }
      }
       this.flag = false;
       this.notifyAll();
       this.id = i;// 生产了第几只产品
  }
   public synchronized void pop(){
       if(flag){
           try {
               this.wait();
          } catch (InterruptedException e) {
               e.printStackTrace();
          }
      }
       this.flag = true;
       this.notifyAll();
       System.out.println("消费了"+id+"只产品");
  }
}

 

posted @ 2020-06-18 16:31  compileTom  阅读(124)  评论(0编辑  收藏  举报