zoukankan      html  css  js  c++  java
  • 生产消费锁---信号灯模式

    package com.Thread;
     
    public class Custom_Producer {
           public static void main(String[] args) {
                 //共享资源
                Production pro = new Production();
                
                Custom custom = new Custom(pro);
                Producer producer = new Producer(pro);
                 new Thread(producer).start();
                 new Thread(custom).start();
          }
    }
    //资源
    class Production {
           private String src ;
           /**
           * 信号
           * flag ---> true  生产,消费等待, 生产完成,通知消费
           * flag ---> false  消费,生产等待,  消费完成,通知生产
           */
           private boolean flag = true;
          
           public synchronized void product(String src) throws Exception{
                 if(!flag ) {// false 生产等待
                       this.wait();
                }
                 //开始生产
                Thread. sleep(500);
                 //生产完毕
                 this.src = src;
                System. out.println("生产了----------->" + src);
                 //通知消费
                 this.notify();
                 //停止生产
                 this.flag = false;
          }
          
           public synchronized void custom() throws Exception {
                 if(flag ) {// true 消费等待
                       this.wait();
                }
                 //开始消费
                System. out.println("消费了---" +src );
                 //消费完毕
                 //通知生产
                 this.notifyAll();
                 //停止消费
                 this.flag = true;
          }
    }
    //生产
    class Producer implements Runnable {
          Production p ;
          
           public Producer(Production p) {
                 this.p = p;
          }
           @Override
           public void run() {
                 for(int i=0; i<20; i++) {
                       if(0==i%2) {
                             try {
                                   p.product( "奇数====生产了" + i +" 个");
                            } catch (Exception e) {
                                  e.printStackTrace();
                            }
                      } else {
                             try {
                                   p.product( "偶数====生产了" + i +" 个");
                            } catch (Exception e) {
                                  e.printStackTrace();
                            }
                      }
                }
          }
          
    }
    //消费
    class Custom implements Runnable {
          Production p ;
     
           public Custom(Production p) {
                 this.p = p;
          }
           @Override
           public void run() {
                 for(int i=0; i<20; i++) {
                       try {
                             p.custom();
                      } catch (Exception e) {
                            e.printStackTrace();
                      }
                }
          }
    }
  • 相关阅读:
    C语言——enum
    C语言——杂实例
    pc上用C语言模拟51多任务的案例程序
    C语言结构体实例-创建兔子
    C语句模拟多任务实例
    求解1^2+2^2+3^2+4^2+...+n^2的方法(求解1平方加2平方加3平方...加n平方的和)
    啊啊啊哦哦
    2013多校联合3 G The Unsolvable Problem(hdu 4627)
    c#操作excel方式三:使用Microsoft.Office.Interop.Excel.dll读取Excel文件
    揭开Socket编程的面纱
  • 原文地址:https://www.cnblogs.com/king-/p/4389734.html
Copyright © 2011-2022 走看看