Condition类_demo

参考:

https://blog.csdn.net/u014082714/article/details/83927697

https://blog.csdn.net/a1439775520/article/details/98471610

Resource.java

package com.hmb;

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

public class Resource {
    private Lock lock = new ReentrantLock();
    private Condition notFull = lock.newCondition();
    private Condition notEmpty = lock.newCondition();
    private int queueSize = 10;
    private PriorityQueue<Integer> pq = new PriorityQueue<>(queueSize);

    public void consume() {
        lock.lock();
        try {
            while (pq.isEmpty()) {
                System.out.println(Thread.currentThread().getName() + "pq is empty, waiting...");
                notEmpty.await();
            }

            pq.poll();
            notFull.signal();
            System.out.println(Thread.currentThread().getName() + "consume a product, pq size: " + pq.size());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void product() {
        lock.lock();
        try {
            while (pq.size() >= queueSize) {
                System.out.println(Thread.currentThread().getName() + "pq is full, waiting...");
                notFull.await();
            }

            pq.offer(1);
            notEmpty.signal();
            System.out.println(Thread.currentThread().getName() + "produce a product, pq size:" + pq.size());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}

  

Producer.java

package com.hmb;

public class Producer implements Runnable{
    private Resource resource;
    private volatile int time = 0;

    public Producer(Resource resource) {
        this.resource = resource;
    }

    @Override
    public void run() {
        while (time < 10) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            resource.product();
            time++;
        }
    }
}

  

Consumer.java

package com.hmb;

public class Consumer implements Runnable{
    private Resource resource;
    private volatile int time = 0;

    public Consumer(Resource resource) {
        this.resource = resource;
    }

    @Override
    public void run() {
        while (time < 10) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            resource.consume();
            time++;
        }
    }
}

  

Main.java

package com.hmb;

public class Main {
    public static void main(String[] args) {
        Resource resource = new Resource();
        new Thread(new Producer(resource)).start();
        new Thread(new Producer(resource)).start();
        new Thread(new Producer(resource)).start();
        new Thread(new Consumer(resource)).start();
        new Thread(new Consumer(resource)).start();
    }
}

  

运行效果:

 

posted @ 2023-06-13 11:42  hemeiwolong  阅读(7)  评论(0编辑  收藏  举报