传统消费者、生产者模型 syncrinized和Object的wait,notifyAll实现

传统模型
package suanfa;

import java.util.Date;
import java.util.LinkedList;

public class Main {
	public static void main(String[] args) throws Exception {
		Buffer buffer = new Buffer(5);
		for(int i =1;i<=3;i++){
			new Thread(new Producer(buffer)).start();
			new Thread(new Consumer(buffer)).start();
		}
	}
}

class Buffer{
	int maxSize;
	LinkedList<Date> list;
	public Buffer(int maxSize){
		this.maxSize = maxSize;
		list = new LinkedList<Date>();
		
	}
	public synchronized void put() throws Exception{
		while(list.size() == maxSize){
			this.wait();
		}
		list.push(new Date());
		System.out.println(Thread.currentThread().getName()+"生产了商品, 当前商品数:"+list.size());
		//唤醒所有消费者
		this.notifyAll();
	}
	public synchronized void take() throws Exception{
		//为什么用while?:notifyAll会唤醒全部消费者、生产者线程,唤醒后的执行顺序是随机的,若消费者抢到了执行权
		//此时列表数目为0,不能消费,所以继续休眠
		while(list.size() == 0){
			this.wait();
		}
		list.pop();
		System.out.println(Thread.currentThread().getName()+"消费了商品, 当前商品数:"+list.size());
		//唤醒所有生产者
		this.notifyAll();
	}
}
class Producer implements Runnable{
	Buffer buffer;
	public Producer(Buffer buffer){
		this.buffer = buffer;
	}
	@Override
	public void run() {
		while(true){
			try {
				buffer.put();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
class Consumer implements Runnable{
	Buffer buffer;
	public Consumer(Buffer buffer){
		this.buffer = buffer;
	}
	@Override
	public void run() {
		while(true){
			try {
				buffer.take();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}



Lock、Condition模型
package suanfa;

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

public class Main {
	public static void main(String[] args) throws Exception {
		Buffer buffer = new Buffer(5);
		for (int i = 1; i <= 3; i++) {
			new Thread(new Producer(buffer)).start();
			new Thread(new Consumer(buffer)).start();
		}
	}
}

class Buffer {
	int maxSize;
	LinkedList<Date> list;
	ReentrantLock lock;
	//与传统模型的不同之处,以免notifyAll唤醒全部等待线程,抽象两个阻塞队列,这样消费者可以只唤醒全部的生产者,
	//生产者也可以只唤醒全部的消费者
	Condition producer;
	Condition consumer;

	public Buffer(int maxSize) {
		lock = new ReentrantLock();
		this.maxSize = maxSize;
		list = new LinkedList<Date>();
		producer = lock.newCondition();
		consumer = lock.newCondition();
	}

	public void put() throws Exception {
		lock.lock();
		try {
			while (list.size() == maxSize) {
				producer.await();
			}
			list.push(new Date());
			System.out.println(Thread.currentThread().getName() + "生产了商品, 当前商品数:" + list.size());
			// 唤醒所有消费者
			consumer.signalAll();
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			if(lock.isHeldByCurrentThread()){
				lock.unlock();
			}
		}
	
	}

	public  void take() throws Exception {
		lock.lock();
		try {
			while (list.size() == 0) {
				consumer.await();
			}
			list.pop();
			System.out.println(Thread.currentThread().getName() + "生产了商品, 当前商品数:" + list.size());
			// 唤醒所有消费者
			producer.signalAll();
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			if(lock.isHeldByCurrentThread()){
				lock.unlock();
			}
		}
	}
}

class Producer implements Runnable {
	Buffer buffer;

	public Producer(Buffer buffer) {
		this.buffer = buffer;
	}

	@Override
	public void run() {
		while (true) {
			try {
				buffer.put();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}

class Consumer implements Runnable {
	Buffer buffer;

	public Consumer(Buffer buffer) {
		this.buffer = buffer;
	}

	@Override
	public void run() {
		while (true) {
			try {
				buffer.take();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个基于Java和Swing的生产者消费者模型时间片轮转算法的窗口实现完整代码: ``` import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.LinkedList; import java.util.Queue; import java.util.Random; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.SwingUtilities; import javax.swing.Timer; public class ProducerConsumer extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; private static final int WIDTH = 600; private static final int HEIGHT = 400; private static final int BUFFER_SIZE = 10; private static final int PRODUCER_DELAY = 1000; private static final int CONSUMER_DELAY = 2000; private static final int TIME_QUANTUM = 100; private static final int MAX_PRODUCE = 3; private static final int MAX_CONSUME = 2; private static final int PRODUCE_BUTTON_INDEX = 0; private static final int CONSUME_BUTTON_INDEX = 1; private static final String PRODUCE_BUTTON_TEXT = "Start Producer"; private static final String CONSUME_BUTTON_TEXT = "Start Consumer"; private static final String PRODUCE_STATUS_TEXT = "Producer Status: %s"; private static final String CONSUME_STATUS_TEXT = "Consumer Status: %s"; private static final String BUFFER_STATUS_TEXT = "Buffer Status: %d/%d"; private static final String LOG_FORMAT = "[%s] %s %d\n"; private static final String EMPTY_BUFFER_INDICATOR = "-"; private final Object lock = new Object(); private final Random random = new Random(); private final Queue<Integer> buffer = new LinkedList<>(); private final Timer producerTimer = new Timer(PRODUCER_DELAY, this); private final Timer consumerTimer = new Timer(CONSUMER_DELAY, this); private final JTextArea logArea = new JTextArea(); private final JLabel produceStatusLabel = new JLabel(String.format(PRODUCE_STATUS_TEXT, "Stopped")); private final JLabel consumeStatusLabel = new JLabel(String.format(CONSUME_STATUS_TEXT, "Stopped")); private final JLabel bufferStatusLabel = new JLabel(String.format(BUFFER_STATUS_TEXT, 0, BUFFER_SIZE)); private final JButton[] buttons = { new JButton(PRODUCE_BUTTON_TEXT), new JButton(CONSUME_BUTTON_TEXT) }; private boolean producerRunning = false; private boolean consumerRunning = false; private int timeLeft = TIME_QUANTUM; public ProducerConsumer() { super("Producer Consumer Simulation"); setPreferredSize(new Dimension(WIDTH, HEIGHT)); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new BorderLayout()); JPanel controlPanel = new JPanel(new FlowLayout()); for (JButton button : buttons) { button.addActionListener(this); controlPanel.add(button); } JPanel statusPanel = new JPanel(new FlowLayout()); statusPanel.add(produceStatusLabel); statusPanel.add(consumeStatusLabel); statusPanel.add(bufferStatusLabel); JPanel logPanel = new JPanel(new BorderLayout()); logPanel.add(new JLabel("Log:"), BorderLayout.NORTH); logPanel.add(logArea, BorderLayout.CENTER); add(controlPanel, BorderLayout.NORTH); add(statusPanel, BorderLayout.CENTER); add(logPanel, BorderLayout.SOUTH); pack(); setLocationRelativeTo(null); setVisible(true); } private void produce() { int count = random.nextInt(MAX_PRODUCE) + 1; synchronized (lock) { while (buffer.size() + count > BUFFER_SIZE) { try { lock.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } for (int i = 0; i < count; i++) { buffer.add(random.nextInt(100)); } bufferStatusLabel.setText(String.format(BUFFER_STATUS_TEXT, buffer.size(), BUFFER_SIZE)); logArea.append(String.format(LOG_FORMAT, "PRODUCE", "Produced", count)); lock.notifyAll(); } } private void consume() { int count = random.nextInt(MAX_CONSUME) + 1; synchronized (lock) { while (buffer.size() < count) { try { lock.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } for (int i = 0; i < count; i++) { buffer.poll(); } bufferStatusLabel.setText(String.format(BUFFER_STATUS_TEXT, buffer.size(), BUFFER_SIZE)); logArea.append(String.format(LOG_FORMAT, "CONSUME", "Consumed", count)); lock.notifyAll(); } } private void runProducer() { producerRunning = true; produceStatusLabel.setText(String.format(PRODUCE_STATUS_TEXT, "Running")); producerTimer.start(); } private void runConsumer() { consumerRunning = true; consumeStatusLabel.setText(String.format(CONSUME_STATUS_TEXT, "Running")); consumerTimer.start(); } private void stopProducer() { producerRunning = false; produceStatusLabel.setText(String.format(PRODUCE_STATUS_TEXT, "Stopped")); producerTimer.stop(); } private void stopConsumer() { consumerRunning = false; consumeStatusLabel.setText(String.format(CONSUME_STATUS_TEXT, "Stopped")); consumerTimer.stop(); } private void logEmptyBuffer() { logArea.append(String.format(LOG_FORMAT, "CONSUME", "Buffer is empty", 0)); } private void runTimeSlice() { timeLeft -= TIME_QUANTUM; if (timeLeft <= 0) { timeLeft = TIME_QUANTUM; synchronized (lock) { lock.notifyAll(); } } } @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == buttons[PRODUCE_BUTTON_INDEX]) { if (producerRunning) { stopProducer(); } else { runProducer(); } } else if (e.getSource() == buttons[CONSUME_BUTTON_INDEX]) { if (consumerRunning) { stopConsumer(); } else { runConsumer(); } } else if (e.getSource() == producerTimer) { produce(); } else if (e.getSource() == consumerTimer) { synchronized (lock) { if (buffer.isEmpty()) { logEmptyBuffer(); lock.notifyAll(); } else { consume(); } } } runTimeSlice(); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new ProducerConsumer(); } }); } } ``` 在这个实现中,我们使用了两个Timer对象来模拟生产者消费者的生产和消费行为。每当Timer对象触发ActionEvent时,我们会调用相应的生产或消费方法。 对于生产方法,我们随机生成一定数量的数字,并把它们添加到缓冲区中。如果缓冲区已满,我们会等待直到有足够的空间来添加新的数字。当生产完成后,我们会更新缓冲区状态并记录日志,然后唤醒任何等待的线程。 对于消费方法,我们随机生成一定数量的数字,并从缓冲区中移除它们。如果缓冲区为空,我们会记录日志并唤醒任何等待的线程。当消费完成后,我们会更新缓冲区状态并记录日志,然后唤醒任何等待的线程。 我们还实现了时间片轮转算法,并在每个时间片结束时唤醒任何等待的线程。这是为了确保在生产者消费者之间公平地分配CPU时间。 最后,我们使用Swing创建了一个简单的GUI界面,使用户能够启动和停止生产者消费者。我们还在界面上显示了生产者消费者的状态以及缓冲区的状态和日志。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值