JAVA-多线程机制中关于生产者消费者模型

关于多线程的问题

1.  关于多线程生产者的职责是什么?

如果共享数据没有被消费,则生产者等消费者消费;生产者被唤醒生产后,要通知消费者

2.  关于多线程消费者的职责是什么?

如果共享数据已没有了,则消费者等待生产者生产,消费者被唤醒消费后,要通知生产者

代码如下:

//PC.java  by kakasi in 20160102

import static common.IO.*;	//println(xxxx)=System.out.println(xxxx)

//sign
enum State{PUTOVER, GETOVER, UNKNOWN};

//shared data
class Data{
	private int count = 0;
	private boolean isEmpty = true;
	private State state = State.UNKNOWN;
	
	public synchronized void get() throws Exception{
		//empty, then wait or return directly
		if (isEmpty){
			//put is over, return directly
			if (state == State.PUTOVER){
				state = State.GETOVER;
				println("put is over");
				println("get is over.");
				return;
			}
			//put is not over, then wait
			println("consumer wait.");
			wait();	
		}
		//not empty
		this.count--;	//consume only one every time
		if (this.count == 0){
			isEmpty = true;
		}
		println("get() " + 1);	//consumer information
		println("after get(), remains " + this.count);
		notify();
	}
	
	public synchronized void put(int count) throws Exception{
		//not empty, then wait
		if (!isEmpty){
			println("producer wait.");
			wait();
		}
		//empty
		this.count = count;
		if (this.count > 0){
			isEmpty = false;
		}
		println("after put(), remains " + count);
		notify();
	}
	
	public void setState(State state){
		this.state = state;
	}
	
	public State getState(){
		return state;
	}
}

//producer
class Producer implements Runnable{
	private Data data;
	
	public Producer(Data data){
		this.data = data;
	}
	
	public void run(){
		for(int i = 1; i < 5; i++){
			try{
				data.put(i);
			}catch(Exception e){}
		}
		//state = PUTOVER
		this.data.setState(State.PUTOVER);
	}
}

//consumer
class Consumer implements Runnable{
	private Data data;
	
	public Consumer(Data data){
		this.data = data;
	}
	
	public void run(){
		//loop while state != GETOVER
		while(this.data.getState() != State.GETOVER){
			try{
				data.get();
			}catch(Exception e){}
		}
	}
}

//runner
class Test{
	public static void main(String[] args) throws Exception{
		Data data = new Data();
		new Thread(new Producer(data)).start();
		new Thread(new Consumer(data)).start();
	}
}

结果如下:


转载请注明出处,谢谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个使用Java语言实现生产者和消费模型的示例: ``` import java.util.LinkedList; public class ProducerConsumerExample { public static void main(String[] args) { LinkedList<Integer> queue = new LinkedList<>(); int maxSize = 5; Producer producer = new Producer(queue, maxSize); Consumer consumer = new Consumer(queue); Thread producerThread = new Thread(producer, "Producer"); Thread consumerThread = new Thread(consumer, "Consumer"); producerThread.start(); consumerThread.start(); } } class Producer implements Runnable { private LinkedList<Integer> queue; private int maxSize; public Producer(LinkedList<Integer> queue, int maxSize) { this.queue = queue; this.maxSize = maxSize; } @Override public void run() { while (true) { synchronized (queue) { while (queue.size() == maxSize) { try { System.out.println("队列已满,生产者等待消费..."); queue.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } int number = (int) (Math.random() * 100); queue.add(number); System.out.println("生产生产: " + number); queue.notifyAll(); } } } } class Consumer implements Runnable { private LinkedList<Integer> queue; public Consumer(LinkedList<Integer> queue) { this.queue = queue; } @Override public void run() { while (true) { synchronized (queue) { while (queue.isEmpty()) { try { System.out.println("队列为空,消费者等待生产..."); queue.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } int number = queue.removeFirst(); System.out.println("消费消费: " + number); queue.notifyAll(); } } } } ``` 上述代码实现了一个简单的生产者和消费模型,其使用了一个线程安全的`LinkedList`队列作为生产者和消费者之间的缓冲区。在`Producer`和`Consumer`类,`run()`方法被覆盖并实现生产消费的逻辑。 在生产者线程,如果队列已满,则生产者线程将进入等待状态。当队列不满时,生产者线程将生成随机数并将其添加到队列,并通过调用`notifyAll()`方法通知消费者线程可以消费了。在消费者线程,如果队列为空,则消费者线程将进入等待状态。当队列不为空时,消费者线程将从队列删除第一个元素,并通过调用`notifyAll()`方法通知生产者线程可以继续生产。 这种实现方式使用`synchronized`关键字确保在对队列进行修改时线程安全。此外,生产者和消费者线程之间的通信使用了`wait()`和`notifyAll()`方法,以确保生产者和消费者之间的协调。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值