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();
      }
    }
    

      

  • 相关阅读:
    Git 命令 stash cherry-pick reset rebase
    【操作系统】不同语言的线程实现机制对比及数据库锁问题
    【数据结构】搜索二叉树(BST)和普通二叉树的序列化与反序列化
    【自制编译器】读书笔记 -- JavaCC使用的JJ文件格式
    leetcode 874 Robot Simulation
    hihocoder 编程挑战赛75
    浪漫主义的起源--以赛亚柏林
    【美团笔试 2018-4-20 】编程题-1 测例100%通过 欧拉函数求解gcd
    【POJ SOJ HDOJ】HDU 2196 Computer 树中的最长路径
    【Java 核心】多线程笔记
  • 原文地址:https://www.cnblogs.com/chen--biao/p/11371051.html
Copyright © 2011-2022 走看看