首页 > 编程语言 > 详细

java--JUC--Lock之Condition

时间:2021-06-04 22:36:07      阅读:21      评论:0      收藏:0      [点我收藏+]
  1. 技术分享图片
  2.  

    synchronized:wait和notify

  3. lock:await和signal 

  4. notify唤醒所有等待的线程,这些线程疯抢,谁抢到是谁的
  5. signal能够精确的唤醒某个线程
  6. 实际系统之间系统调度举例:
    1. package com.model.concurrent;
      
      import java.util.concurrent.locks.Condition;
      import java.util.concurrent.locks.Lock;
      import java.util.concurrent.locks.ReentrantLock;
      
      /**
       * 备注:多线程之间循序调用,实现A->B->C
       * 三个线程,要求如下:
       * A打印五次,B打印十次,C打印十五次
       * 接着
       * A打印五次,B打印十次,C打印十五次
       * 来十轮
       *
       *
       * */
      class Print{
      
          private int num=1;
          private Lock lock=new ReentrantLock();
          private Condition c1=lock.newCondition();
          private Condition c2=lock.newCondition();
          private Condition c3=lock.newCondition();
      
          public void print_a(){
              lock.lock();
              try {
              if (num!=1){
                  c1.await();
              }
              for (int i=0;i<5;i++) {
                  System.out.println("A\t"+i);
              }
              num++;
              c2.signal();
              }catch (Exception e){
                  e.printStackTrace();
              }finally {
                  lock.unlock();
              }
          }
          public void print_b(){
              lock.lock();
              try {
              if (num!=2){
                  c2.await();
              }
              for (int i=0;i<10;i++) {
                  System.out.println("B\t"+i);
              }
              num++;
              c3.signal();
              }catch (Exception e){
                  e.printStackTrace();
              }finally {
                  lock.unlock();
              }
          }
          public void print_c(){
              lock.lock();
              try {
              if (num!=3){
                  c3.await();
              }
              for (int i=0;i<15;i++) {
                  System.out.println("C\t"+i);
              }
              num=1;
              c1.signal();
              }catch (Exception e){
                  e.printStackTrace();
              }finally {
                  lock.unlock();
              }
          }
      }
      public class ConditionDemo {
          public static void main(String[] args) {
              Print print=new Print();
              new Thread(() ->{
                  for (int i = 0; i < 10; i++) {
                      print.print_a();
                  }
              },"A").start();
      
              new Thread(() ->{
                  for (int i = 0; i < 10; i++) {
                      print.print_b();
                  }
              },"B").start();
              new Thread(() ->{
                  for (int i = 0; i < 10; i++) {
                      print.print_c();
                  }
              },"C").start();
          }
      }

       

          

java--JUC--Lock之Condition

原文:https://www.cnblogs.com/zzhAylm/p/14851356.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!