java生产者消费者模式实现

本文介绍了一种使用Java实现的多线程生产者消费者模式。通过自定义队列和利用阻塞队列的方式实现了生产者与消费者的同步操作。生产者负责生产数据并填充到队列中,而消费者则从队列中取出数据进行消费。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.thread;

import java.util.LinkedList;

public class MuitlProduceAndTake {

	public static void main(String[] args) {
		service service = new service();

		for (int i = 0; i < 3; i++) {
			new Thread(new Runnable() {

				@Override
				public void run() {
					service.produce();
				}
			}).start();
			
			new Thread(new Runnable() {
				
				@Override
				public void run() {
					service.get();
				}
			}).start();
		}
	}

}

class service {

	private final int MAX_SIZE = 5;

	private LinkedList<Object> list = new LinkedList<Object>();

	public void produce() 
	{
		synchronized (list) 
		{
			System.out.println("开始生产!");
			if (list.size() + 1 > MAX_SIZE) {
				System.out.println("已经满了,list不能再放了");
				try {
					list.wait();
					System.out.println("满了,produce正在阻塞");

				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			list.add("ss");
			System.out.println("增加了1,目前大小是:" + list.size());
			list.notifyAll();
		}
	}

	public void get()
	{
		//进入循环后会独占list资源
		synchronized (list)
		{
			System.out.println("开始获取!");
			if (list.size() <= 0)
			{
				System.out.println("库存为0,还不能获取!");
				try {
					//表示放弃对list资源的独占
					list.wait();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			list.remove();
			System.out.println("取走了1,目前是"+list.size());
			list.notifyAll();
		}
	}
}
方式二:
package com.thread;

import java.util.Random;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class Test {

	 public static void main(String[] args)
	 {
		 services service=new services();
		
		 try {
			new Thread().wait();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 new Thread().notifyAll();
		 
		 /*for (int i = 0; i < 15; i++) {
				new Thread(new Runnable() {

					@Override
					public void run() {
						service.Produce();
					}
				}).start();
				
				new Thread(new Runnable() {
					
					@Override
					public void run() {
						service.get();
					}
				}).start();
			}*/
	 }
}

class services{
	
	final int Max_SIZE=5;
	
	BlockingQueue<String> queue=new ArrayBlockingQueue<>(5);
	
	public  synchronized void get()
	{
		if(queue.size()<=0)
		{
			System.out.println("库存为0还不能获取");
			try {
				System.out.println("get放弃资源");
				this.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		try {
			String name=queue.take();
			System.out.println("获取数据"+name);
			this.notifyAll();
			System.out.println("get唤醒线程");
			
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public synchronized void Produce()
	{
		if(queue.size()>Max_SIZE)
		{
			System.out.println("已经满了,不能在生产了!");
			try {
				System.out.println("produce放弃资源!");
				this.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		String name=new Random().nextInt(1000)+"";
		try {
			System.out.println("准备生产数据!");
			queue.put(name);
			System.out.println("已放入数据"+name);
			this.notifyAll();
			System.out.println("peoduce开始唤醒线程");
			
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值