zoukankan      html  css  js  c++  java
  • 生产者消费/单例设计模式/线程协作

      死锁。(使用sysnchronized易造成死锁,用线程协作解锁 )
      lock。
     
      同步:
        a.同步实例方法 this
        b.同步块       指定对象(临界资源)监视器
        c.同步静态方法 Class   对象监视器
             Class oclz =new Object().getClass();  
     
    /**
    *单例设计模式
    */
    publc class A{
         private stsitc A a;
         private A(){
         try{
              Thread.sleep(100);          
         }catch(InterruptedException e){
              e.printStackRrace();
         }    
           System.out.println("====a====="); 
         }
         public static A getInstance(){
             if(a==null){
                   a=new A();
              }
              return a;
         }
    }
     
     
    /**
    *懒汉模式  ---------- 
    */
    publc class AA{
         private stsitc AA aa;
         private A(){
         try{
              Thread.sleep(100);
         }catch(InterruptedException e){
              e.printStackRrace();
         }
           System.out.println("====aa=====");
         }
         public synchronized static AA getInstance(){     //性能差
             if(a==null){
                   a=new AA();
              }
              return a;
         }
    }
     
     
    /**  
    *饿汉模式  ---------- 【消耗资源】
    */
    publc class AAA{
         private static AAA aaa = new AAA();          //jvm 自身携带的监视器(锁)      线程不安全
         private AAA(){
         try{
              Thread.sleep(100);
         }catch(InterruptedException e){
              e.printStackRrace();
         }
           System.out.println("====aa=====");
         }
         public static AAA getInstance(){
              return aaa;
         }
    }
     
     
    /**
    *优化模式  ----------
    */
    publc class AAAA{
         private static class X{                 //引入静态内部类
              private static AAAA a = new AAAA();               
         }
         private AAAA(){
         try{
              Thread.sleep(100);
         }catch(InterruptedException e){
              e.printStackRrace();
         }
           System.out.println("====aaaa=====");
         }
         public static AAAA getInstance(){
              return X.a;
         }
    }
     
     
    /**
    *测试
    */
     
    public ststic void main(String[] args)
        /*
          new Thread(){
              public void run(){               //匿名内部类
                    A.getInstance();          
              }
           }.start();
     
          new Thread(){
              public void run(){
                    A.getInstance();
              }
           }.start(); */
     
          new Thread(){
              public void run(){               //匿名内部类
                    AA.getInstance();
              }
           }.start();
     
          new Thread(){
              public void run(){
                    AA.getInstance();
              }
           }.start();
    }
     
     
    协助: wait(让线程在同步块内部释放指定对象的监视器)/notifyAll()
    意义? 可以让线程在同步代码块释放对象监视器。
     
    案例:  生产者,消费者。 
     
     
    /**
    *生产者,消费者模式
    */
    public class Box{
         private int size;
         private int count;
         public Box(int size){
              this.size = size;
         }     
         public boolean isEmpty(){
              return count<=0;
         }
         public boolean isEmpty(){
              return count>=size;
         }
         private void add(){
              count++;
         System.out.println(Thread.currentThread().getNmae()+"========生产了一个面包=========当前数量"+count);
         }  
     
         private void jian(){
              count--;
         System.out.println(Thread.currentThread().getNmae()+"========消费了一个面包=========当前数量"+count);
         }
       
    }
     
     
     
    //生产者
     
    public class Marker extends Thead{
        private Box box;
     
        public Marker(Box box,String name){
              super(name);
              this.box = box;
         }
     
     
         public void run(){
              for(int i = 0; i<1000;i++){
                   try(){
                       sysnchronized(box){
                            while(box.isFull()){
         box.wait();
         }
         box.add();
         box.notufyAll();   //唤醒,进入等待池
    }
    }catch(Exception e){
         e.printStackTrace();
    }
                        }
                   }
              }
         }
    }
     
     
     
    //消费者
     
    public class Seller extends Thead{
        private Box box;
     
        public Marker(Box box,String name){
              super(name);
              this.box = box;
         }
     
         public void run(){
              for(int i = 0; i<1000;i++){
                   try(){
                       sysnchronized(box){
                            while(box.isEmpty()){
         box.wait();
         }
         box.jian();
         box.notufyAll();          //唤醒,消费者进入等待池
    }
    }catch(Exception e){
         e.printStackTrace();
    }
                   }
              }
         }
    }
     
     
    /**
    *测试
    */
    public class Test{
         public static void main(String[]args){
              Box b = new Box(10);
              Maker m1 = new Marker(b,"sc--001");
              Seller s1 = new Seller(b,"xf=======001");
         
              m1.start();
              s1.start();
         }
    }
     
     
     
     
     
  • 相关阅读:
    机器学习算法及应用领域相关的中国大牛[转]
    Awesome (and Free) Data Science Books[转]
    机器学习算法之旅【翻译】【转】
    const 引用的分析
    c++ 引用的分析
    读取BMP图像size的时候与操作和左移的原因
    java的equal和==问题
    mac10.9下安装Android
    c++设计模式系列----builder模式
    c++设计模式系列----单例模式(Singleton模式
  • 原文地址:https://www.cnblogs.com/nin-w/p/5907472.html
Copyright © 2011-2022 走看看