用代码实现生产者消费者模型

题目:代码实现生产者消费者模型,3个生产者每隔3秒生产1个单位的数据,2个消费者每隔1秒消费一个单位的数据,共享资源是有界的,可以用数组或队列。要求不能出现死锁或者占用CPU不释放的情况。

方法一:用synchronized关键字加锁实现

public class TestMain {
    // 共享资源
    public static class Resource {
        private LinkedList<String> commonList;
        private int capacity = 0;

        public Resource(int capacity) {
            this.capacity = capacity;
            this.commonList = new LinkedList<>();
        }

        public void addLast(String item) {
            commonList.addLast(item);
        }

        public String getFirst() {
            return commonList.getFirst();
        }

        public void removeFirst() {
            commonList.removeFirst();
        }

        public int size() {
            return commonList.size();
        }

        public boolean isFull() {
            return size() == this.capacity;
        }
    }

    // 生产者
    public static class Producer extends Thread {
        private int      duration;
        private Resource resource;

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

        @Override
        public void run() {
            while (true) {
                try {
                    synchronized (resource) {
                        if (!resource.isFull()) {
                            resource.addLast("Producer_" + Thread.currentThread().getName());
                            System.out.println("produced_Resource.size() = " + resource.size());
                            // notify/notifyAll() 方法,唤醒一个或多个正处于等待状态的线程
                            resource.notifyAll();
                        } else {
                            System.out.println("resource is full, waiting...");
                            // wait()使当前线程阻塞,前提是必须先获得锁配合synchronized 关键字使用
                            resource.wait();
                        }
                    }
                    Thread.sleep(1000 * duration);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    // 消费者
    public static class Consumer extends Thread {
        private int   duration;
        private Resource resource;

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

        @Override
        public void run() {
            while (true) {
                try {
                    synchronized (resource) {
                        // 共享区有数据才能取数据
                        if (resource.size() > 0) {
                            System.out.println("Consumed_resource.size() = " + resource.size());
                            resource.removeFirst();
                            // 消费数据后唤醒生产者
                            resource.notifyAll();
                        } else {
                            System.out.println("resource is empty, waiting...");
                            // wait()使当前线程阻塞,前提是 必须先获得锁,配合synchronized 关键字使用
                            resource.wait();
                        }
                    }
                    Thread.sleep(1000 * duration);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {
        Resource resource = new Resource(10);
        new Producer(3, resource).start();
        new Producer(3, resource).start();
        new Producer(3, resource).start();
        
        new Consumer(1, resource).start();
        new Consumer(1, resource).start();
        System.out.println("Hello World!");
    }
}

方法二:用阻塞队列实现

用阻塞队列来充当共享资源,好处是代码中不用再显式的处理锁,例如ArrayBlockingQueue内部就已经使用了一个ReentrantLock和两个Condition来实现了对put操作与take操作的阻塞/唤醒。这样写的前提是你知道jdk的BlockingQueue解决了多线程环境下的数据共享。

// 生产者
public class Producer extends Thread {
    // 间隔时间
    private int   duration;
    private BlockingQueue<String> blockingQueue;

    public Producer (int duration, BlockingQueue<String> blockingQueue) {
        this.duration = duration;
        this.blockingQueue = blockingQueue;
    }

    @Override
    public void run() {
        while (true) {
            try {
                // 队列满时,put操作将被阻塞直到队列有空间为止
                blockingQueue.put("Producer_" + Thread.currentThread().getName());
                Thread.sleep(1000 * duration);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

// 消费者
public class Consumer extends Thread {
    // 间隔时间
    private int                   duration;
    private BlockingQueue<String> blockingQueue;

    public Consumer (int duration, BlockingQueue<String> blockingQueue) {
        this.duration = duration;
        this.blockingQueue = blockingQueue;
    }

    @Override
    public void run() {
        while (true) {
            try {
                String e = blockingQueue.take();
                Thread.sleep(1000 * duration);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}


public class TestMain {
    public static void main(String[] args) {
        BlockingQueue<String> resource = new ArrayBlockingQueue<>(24);
        new Producer(3, resource).start();
        new Producer(3, resource).start();
        new Producer(3, resource).start();
        new Consumer(1, resource).start();
        new Consumer(1, resource).start();
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
生产者消费者模型是一种经典的多线程同步模型,它描述了两类线程共享同一缓冲区的情形:生产者线程往缓冲区里添加数据,而消费者线程则从缓冲区里取出数据。以下是一个简单的生产者消费者模型代码实现: ```python import threading import time import random class Producer(threading.Thread): def __init__(self, buffer): super().__init__() self.buffer = buffer def run(self): while True: item = random.randint(1, 10) self.buffer.put(item) print(f"Producer produced item {item}") time.sleep(random.random()) class Consumer(threading.Thread): def __init__(self, buffer): super().__init__() self.buffer = buffer def run(self): while True: item = self.buffer.get() print(f"Consumer consumed item {item}") time.sleep(random.random()) class Buffer: def __init__(self, size): self.size = size self.items = [] self.lock = threading.Lock() self.empty = threading.Semaphore(size) self.full = threading.Semaphore(0) def put(self, item): self.empty.acquire() self.lock.acquire() self.items.append(item) self.lock.release() self.full.release() def get(self): self.full.acquire() self.lock.acquire() item = self.items.pop(0) self.lock.release() self.empty.release() return item if __name__ == "__main__": buffer = Buffer(5) producer = Producer(buffer) consumer = Consumer(buffer) producer.start() consumer.start() ``` 这个代码实现了一个简单的生产者消费者模型,其中 `Producer` 和 `Consumer` 是两个线程类,分别代表生产者消费者。`Buffer` 类则是缓冲区,其中使用了两个信号量 `empty` 和 `full` 来分别表示缓冲区的空位和已有数据的数量。`Producer` 线程在运行时会不断往缓冲区里添加随机数,而 `Consumer` 线程则会不断从缓冲区里取出数据并打印。整个程序会一直运行直到手动终止。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值