zoukankan      html  css  js  c++  java
  • condition的使用

    condition 的作用:条件锁

    需求:

      按需执行三个线程。

    用wait,notify的方式:  

    /**
     * 有序线程 wait,notify版
     */
    public class OrderThreadW_NVersion {
      private int flag=0;
    
      public synchronized void A(){
        while (flag != 0){
          try {
            wait();
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
        System.out.println("A");
        flag=1;
        notifyAll();
      }
      public synchronized void B(){
        while (flag != 1){
          try {
            wait();
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
        System.out.println("B");
        flag=2;
        notifyAll();
      }
      public synchronized void C(){
        while (flag != 2){
          try {
            wait();
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
        System.out.println("C");
        flag=0;
        notifyAll();
      }
    
    
      public static void main(String[] args) {
        OrderThreadW_NVersion ot = new OrderThreadW_NVersion();
        new Thread(()->{
          while (true){
            ot.A();
            try {
              Thread.sleep(500);
            } catch (InterruptedException e) {
              e.printStackTrace();
            }
          }
        }).start();
        new Thread(()->{
          while (true){
            ot.B();
            try {
              Thread.sleep(500);
            } catch (InterruptedException e) {
              e.printStackTrace();
            }
          }
        }).start();
        new Thread(()->{
          while (true){
            ot.C();
            try {
              Thread.sleep(500);
            } catch (InterruptedException e) {
              e.printStackTrace();
            }
          }
        }).start();
      }
    }
    

    缺点:

      notifyAll,会唤醒所有等待的线程,不能指定特定线程的唤醒。此时就引入了condition

    用condition的方式来实现:

    /**
     * 有序线程,conditin版
     */
    public class OrderThreadCondition {
    
      private int flag = 0;
      Lock lock = new ReentrantLock();
      Condition a = lock.newCondition();
      Condition b = lock.newCondition();
      Condition c = lock.newCondition();
    
      public void A() {
        lock.lock();
        while (flag != 0) {
          try {
            a.await();//跟object的wait一样
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
        System.out.println("A");
        flag = 1;
        b.signal();//唤醒指定线程
        lock.unlock();
      }
    
    
      public void B() {
        lock.lock();
        while (flag != 1) {
          try {
            b.await();
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
        System.out.println("B");
        flag = 2;
        c.signal();
        lock.unlock();
      }
    
      public void C() {
        lock.lock();
        while (flag != 2) {
          try {
            c.await();
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
        System.out.println("C");
        flag = 0;
        a.signal();
        lock.unlock();
      }
    
      public static void option(OrderThreadCondition ot,String methodName){
        while (true){
          try {
            Method method = ot.getClass().getMethod(methodName, null);
            method.invoke(ot,null);
            Thread.sleep(1000);
          } catch (Exception e) {
            e.printStackTrace();
            System.exit(-1);
          }
        }
      }
    
      public static void main(String[] args) {
        OrderThreadCondition ot = new OrderThreadCondition();
        new Thread(() -> option(ot,"A")).start();
        new Thread(() -> option(ot,"B")).start();
        new Thread(() -> option(ot,"C")).start();
      }
    }
    

      

  • 相关阅读:
    风云受邀参加CMAX2009技术峰会演讲嘉宾
    银光志—Silverlight 3.0技术详解与最佳实践上架中文亚马逊卓越网和当当网
    Silverlight进度条控件动画源代码
    Silverlight4 Tools下载
    在C#代码中获取Silverlight的初始化initparams参数
    css层的定位position、absolute、relative层叠加的五条叠加法则
    《银光志—Silverlight3.0技术详解与最佳实践》仅上架两周排名互动出版网第三名
    《银光志Silverlight 3.0开发详解与最佳实践》出版电子版——风云编著
    银客帝国招聘Silverlight兼职开发人员
    Silverlight 2使用C#遍历XML(兼容Silverlight3)
  • 原文地址:https://www.cnblogs.com/chen--biao/p/11371051.html
Copyright © 2011-2022 走看看