package com.Demo.Likefr;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Home {
    public static void main(String[] agrs) {
    DATA data=new DATA();

    new Thread(()->{
        try {
            for(int i=0;i<10;i++) {
            data.increment();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    },"A").start();

    new Thread(()->{
        try {
            for(int i=0;i<10;i++) {
            data.decrement();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    },"B").start();
    }
}
class DATA{
    private int number=0;

    Lock lock=new ReentrantLock();
    Condition condition=lock.newCondition();

    public synchronized void increment() throws InterruptedException {
        while(number!=0) {
            this.wait();
        }
        System.out.println(number);
        number++;
        this.notifyAll();
    }

    public synchronized void decrement() throws InterruptedException {
        while(number==0) {
            this.wait();
        }
        System.out.println(number);
        number--;
        this.notifyAll();
    }

}

jdk1.5版本之后的新特性 互斥锁

package com.Demo2;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Demo {
    public static void main(String[] args) {
        Data d = new Data();
        new Thread(()->{
            while(true){
                d.A();
            }
        },"A").start();

        new Thread(()->{
            while(true){
                d.B();
            }
        },"B").start();


        new Thread(()->{
            while(true){
                d.C();
            }
        },"C").start();

    }
}

class Data {
    Lock l = new ReentrantLock();
    Condition condition1 = l.newCondition();
    Condition condition2 = l.newCondition();
    Condition condition3 = l.newCondition();
    private int number = 0;

    public void A() {
        l.lock();
        try {
            //业务代码
            while (number != 0) {
                condition1.await();
            }
            number=1;
            System.out.println(Thread.currentThread().getName()+">>>A");
            condition2.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            l.unlock();
        }
    }

    public void B() {
        l.lock();
        try {
            //业务代码
            while (number != 1) {
                condition2.await();
            }
            number=2;
            System.out.println(Thread.currentThread().getName()+">>>B");
            condition3.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            l.unlock();
        }
    }
    public void C() {
        l.lock();
        try {
            //业务代码
            while (number != 2) {
                condition3.await();
            }
            number=0;
            System.out.println(Thread.currentThread().getName()+">>>C");
            condition1.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            l.unlock();
        }
    }
}
最后修改:2020 年 08 月 22 日
如果觉得我的文章对你有用,请随意赞赏