学习笔记:java多线程JUC之二

生产者消费者(旧)

 /**
  * 题目:现在两个线程,可以操作初始值为零的一个变量,
  * 实现一个线程对该变量加1,一个线程对该变量减1,
  * 实现交替,来10轮,变量初始值为零
  *
  * 1 高内聚低耦合前提下,线程操作资源类
  * 2 判断/干活/通知
  * 3 多线程交互中,必须防止多线程的虚假唤醒,也即(判断只用while,不能用if)
  */
 class AirConditioner
 {
     private int number=0;
     public synchronized void increment() throws InterruptedException{
         while (number!=0){//判断
             this.wait();
        }
         //干活
         number++;
         System.out.println(Thread.currentThread().getName()+" "+number);
         //通知
         this.notifyAll();
    }
     public synchronized void decrement() throws InterruptedException{
         while (number==1){
             this.wait();
        }
         number--;
         System.out.println(Thread.currentThread().getName()+" "+number);
         //通知
         this.notifyAll();
    }
 }
 public class ThreadWaitNotifyDemo {
     public static void main(String[] args) {
         AirConditioner airConditioner = new AirConditioner();
         new Thread(()->{
             for (int i = 0; i < 10; i++) {
                 try {
                     airConditioner.increment();
                }catch (InterruptedException e){
                     e.printStackTrace();
                }
            }
        },"A").start();
         new Thread(()->{
             for (int i = 0; i < 10; i++) {
                 try {
                     airConditioner.decrement();
                }catch (InterruptedException e){
                     e.printStackTrace();
                }
            }
        },"B").start();
    }
 }

生产者消费者(新)

image-20201106224902059

 import com.sun.org.apache.xpath.internal.objects.XNumber;
 
 import java.util.concurrent.locks.Condition;
 import java.util.concurrent.locks.Lock;
 import java.util.concurrent.locks.ReentrantLock;
 
 /**
  * 题目:现在两个线程,可以操作初始值为零的一个变量,
  * 实现一个线程对该变量加1,一个线程对该变量减1,
  * 实现交替,来10轮,变量初始值为零
  *
  * 1 高内聚低耦合前提下,线程操作资源类
  * 2 判断/干活/通知
  * 3 多线程交互中,必须防止多线程的虚假唤醒,也即(判断只用while,不能用if)
  */
 
 class AirConditioner1
 {
     private int number=0;
     private Lock lock=new ReentrantLock();
     private Condition condition=lock.newCondition();
 
     public void increment() throws InterruptedException{
         lock.lock();
         try {
             while (number!=0){
                 condition.await();
            }
             number++;
             System.out.println(Thread.currentThread().getName()+" "+number);
             condition.signalAll();
        }catch (InterruptedException e){
             e.printStackTrace();
        }finally {
             lock.unlock();
        }
    }
 
     public void decrement() throws InterruptedException{
         lock.lock();
         try {
             while (number==0){
                 condition.await();
            }
             number--;
             System.out.println(Thread.currentThread().getName()+" "+number);
             condition.signalAll();
        }catch (InterruptedException e){
             e.printStackTrace();
        }finally {
             lock.unlock();
        }
    }
 }
 public class ThreadWaitNotifyNew {
     public static void main(String[] args) {
         AirConditioner1 airConditioner = new AirConditioner1();
         new Thread(()->{
             for (int i = 0; i < 10; i++) {
                 try {
                     airConditioner.increment();
                }catch (InterruptedException e){
                     e.printStackTrace();
                }
            }
        },"A").start();
 
         new Thread(()->{
             for (int i = 0; i <10; i++) {
                 try{
                     airConditioner.decrement();
                }catch (InterruptedException e){
                     e.printStackTrace();
                }
            }
        },"B").start();
    }
 }