• 生产消费锁---信号灯模式


    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();
                      }
                }
          }
    }
  • 相关阅读:
    簡化SQL Insert、Update、Delete、Select的方法
    Microsoft SqlHelper Class
    EntLib5.0 日志应用程序块(logging) 使用与配置
    EnterpriseLibrary及Log4Net於WebConfig或AppConfig的相關配置
    LogHelper
    log4net使用详解
    Create\Move\Delte Folder\File
    Log4Net使用详解(续)
    DataSet 类(四)读写XML
    SqlDataAdapter类
  • 原文地址:https://www.cnblogs.com/king-/p/4389734.html
Copyright © 2020-2023  润新知