Java并发 | ReentrantLock实现生产者消费者

2019-08-08  本文已影响0人  icebreakeros

ReentrantLock实现生产者消费者

一对一交替打印

class Service {

    private Lock lock = new ReentrantLock();
    private Condition condition = lock.newCondition();
    private boolean hasValue = false;

    public void set() {
        try {
            lock.lock();
            while (hasValue) {
                condition.await();
            }
            System.out.println("★");
            hasValue = true;
            condition.signal();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void get() {
        try {
            lock.lock();
            while (!hasValue) {
                condition.await();
            }
            System.out.println("☆");
            hasValue = false;
            condition.signal();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}

class ProducerOfR implements Runnable {

    private Service service;

    public ProducerOfR(Service service) {
        this.service = service;
    }

    @Override
    public void run() {
        while (true) {
            service.set();
        }
    }
}

class ConsumerOfR implements Runnable {

    private Service service;

    public ConsumerOfR(Service service) {
        this.service = service;
    }

    @Override
    public void run() {
        while (true) {
            service.get();
        }
    }
}

public class Run {

    public static void main(String[] args) throws InterruptedException {
        Service service = new Service();
        Thread pThread = new Thread(new ProducerOfR(service));
        Thread cThread = new Thread(new ConsumerOfR(service));
        pThread.start();
        cThread.start();
    }
}

多对多交替打印

class Service {

    private Lock lock = new ReentrantLock();
    private Condition condition = lock.newCondition();
    private boolean hasValue = false;

    public void set() {
        try {
            lock.lock();
            while (hasValue) {
                System.out.println("★★");
                condition.await();
            }
            System.out.println("★");
            hasValue = true;
            condition.signalAll();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void get() {
        try {
            lock.lock();
            while (!hasValue) {
                System.out.println("☆☆");
                condition.await();
            }
            System.out.println("☆");
            hasValue = false;
            condition.signalAll();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}

class ProducerOfR implements Runnable {

    private Service service;

    public ProducerOfR(Service service) {
        this.service = service;
    }

    @Override
    public void run() {
        while (true) {
            service.set();
        }
    }
}

class ConsumerOfR implements Runnable {

    private Service service;

    public ConsumerOfR(Service service) {
        this.service = service;
    }

    @Override
    public void run() {
        while (true) {
            service.get();
        }
    }
}

public class Run {

    public static void main(String[] args) throws InterruptedException {
        Service service = new Service();
        Thread[] p = new Thread[10];
        Thread[] c = new Thread[10];
        for (int i = 0; i < 10; i++) {
            p[i] = new Thread(new ProducerOfR(service));
            c[i] = new Thread(new ConsumerOfR(service));
            p[i].start();
            c[i].start();
        }
    }
}
上一篇下一篇

猜你喜欢

热点阅读