生产者消费者syn版本和Lock版本+Condition的精确通知

  1. Condition的精确通知


/*
* 题目:多线程之间按顺序来调用,实现A->B->C三个线程启动,要求如下:
* AA打印5次,BB打印10次,Cc打印15次
* 。。。。。。
* 来10轮
* */
class sharedSource{
    private  int num=1;
   Lock lock= new ReentrantLock();
    Condition c1 = lock.newCondition();
    Condition c2 = lock.newCondition();
    Condition c3 = lock.newCondition();
    void print5() {
        lock.lock();
        try{
            //1.判断
            while (num!=1){
                c1.await();
            }
            //2.干活
            for (int i=1;i<=5;i++){
                System.out.println(Thread.currentThread().getName()+"\t"+i);
            }
            //3.通知
            num=2;
            c2.signal();//只通知2号线程
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }
    void  print10(){
        lock.lock();
        try{
            while (num!=2){
                c2.await();
            }
            for (int i=1;i<=10;i++){
                System.out.println(Thread.currentThread().getName()+"\t"+i);
            }
            //3.通知
            num=3;
            c3.signal();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }
    void  print15(){
        lock.lock();
        try{
            while (num!=3){
                c3.await();
            }
            for (int i=1;i<=15;i++){
                System.out.println(Thread.currentThread().getName()+"\t"+i);
            }
            //3.通知
            
            
            num=1;
            c1.signal();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }
}
public class ConditionTest {
    public static void main(String[] args) {
        sharedSource sharedSource = new sharedSource();
        new Thread(()->{
            for (int i=1;i<=10;i++)
            sharedSource.print5();
        },"AA").start();
        new Thread(()->{
            for (int i=1;i<=10;i++)
            sharedSource.print10();
        },"BB").start();
        new Thread(()->{
            for (int i=1;i<=10;i++)
            sharedSource.print15();

        },"CC").start();

    }
}

2.生产者消费者Syn版本

import com.sun.source.tree.SynchronizedTree;

/*题目:
一个初始值为0的变量,两个线程对其交替操作,一个加1,一个减1,来五轮;
* */
//上联:线程 操作 资源类
//下联:判断 干活  再通知
//横批:防止虚假唤醒
public class produceAndCustomerSyn {
    public static void main(String[] args) {
        data1 data = new data1();
        new Thread(()->{
            for (int i=1;i<=5;i++){
                try {
                    data.decrease();
                } catch (Exception e) {
                    e.printStackTrace();
                }}
        },"AA").start();
        new Thread(()->{
            for (int i=1;i<=5;i++){
                try {
                    data.increase();
                } catch (Exception e) {
                    e.printStackTrace();
                }}
        },"BB").start();
        new Thread(()->{
            for (int i=1;i<=5;i++){
                try {
                    data.increase();
                } catch (Exception e) {
                    e.printStackTrace();
                }}
        },"CC").start();
        new Thread(()->{
            for (int i=1;i<=5;i++){
                try {
                    data.decrease();
                } catch (Exception e) {
                    e.printStackTrace();
                }}
        },"DD").start();
    }
}
//资源
class  data1{
    private int num=0;
    //1.操作
      public void  increase() throws InterruptedException {
          synchronized (this) {
              //判断
              while (num != 0) {
                  this.wait();
              }
              //干活
              num++;
              System.out.println(Thread.currentThread().getName() + "\t " + num);
              //通知
              this.notifyAll();
          }
    }
    //1.操作
    public void  decrease() throws InterruptedException {
          //
        synchronized (this) {
            while (num != 1) {
                this.wait();
            }
            num--;
            System.out.println(Thread.currentThread().getName() + "\t " + num);
            this.notifyAll();
        }
    }




}

3.生产者消费者Lock版本

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/*题目:
一个初始值为0的变量,两个线程对其交替操作,一个加1,一个减1,来五轮;
* */
//上联:线程 操作 资源类
//下联:判断 干活  通知
//横批:防止虚假唤醒
public class produceAndCustomerLock{
    public static void main(String[] args) {
        Data data = new Data();
        new Thread(()->{
            for (int i=1;i<=5;i++){
            try {
                data.decrease();
            } catch (Exception e) {
                e.printStackTrace();
            }}
        },"AA").start();
        new Thread(()->{
            for (int i=1;i<=5;i++){
            try {
                data.increase();
            } catch (Exception e) {
                e.printStackTrace();
            }}
        },"BB").start();
    }
}
  //资源
class  Data{
    private int num=0;
    Lock lock=new ReentrantLock();
    Condition condition= lock.newCondition();
    //操作
    void increase() throws Exception {
        lock.lock();
        try {
            //1.判断+防止虚假唤醒
            while (num != 0) {
                condition.await();
            }
            //2.干活
            num++;
            System.out.println(Thread.currentThread().getName() + "\t " + num);
            //3.通知
            condition.signalAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
    void decrease() throws Exception{
        lock.lock();
        try{
            while (num!=1){
                condition.await();
            }
            num--;
            System.out.println(Thread.currentThread().getName()+"\t "+num);
            condition.signalAll();
        }catch (Exception e){
            e.printStackTrace();
        }
    }




}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值