java 生产者消费者练习

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

public class lx4 {
	public static Integer num[] = {0};
	public static Map<String, Thread> map = new HashMap<>();
	public static int s = 0;
	public static int x = 0;
	public static  int h = 0;


	public static void main(String[] args) {
		Long startTime = Calendar.getInstance().getTime().getTime();
		Thread thread1 = new Thread(new ShengChanZhe());
		Thread thread2 = new Thread(new XiaoFeiZhe());
		Thread thread21 = new Thread(new XiaoFeiZhe());
		//Thread thread3 = new Thread(new ZhuangTai());
		thread1.setName("生产者");
		thread2.setName("消费者");
		thread21.setName("黄牛");
		thread2.setPriority(Thread.MIN_PRIORITY);
		thread21.setPriority(Thread.MAX_PRIORITY);
		map.put("s", thread1);
		map.put("x", thread2);
		map.put("x1", thread21);
		thread1.start();
		thread2.start();
		thread21.start();
		//thread3.start();
		Long endTime = Calendar.getInstance().getTime().getTime();
		System.out.println("一共耗时:"+(endTime-startTime));
	}
}

class ShengChanZhe implements Runnable {
	@Override
	public void run() {

		for (int i = 0; i < 10000; i++) {
			/*try {
				Thread.sleep(10);
			}catch (InterruptedException e){
				e.printStackTrace();
			}*/

			synchronized (lx4.num) {
				try {
					lx4.num.notifyAll();
					while (lx4.num[0] >= 40) {
						lx4.num.wait();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				lx4.num[0]++;
				lx4.s++;
				//System.out.println(Thread.currentThread().getName()+"生产了一件商品,剩余:" + lx4.num[0]);


			}
		}
		System.out.println("小米生产了"+lx4.s+"件商品");
		System.out.println("黄牛消费了"+lx4.h+"件商品");
		System.out.println("消费者消费了"+lx4.x+"件商品");

	}
}

class XiaoFeiZhe implements Runnable {
	@Override
	public void run() {
		while (true) {
			synchronized (lx4.num) {
				try {
					lx4.num.notifyAll();
					while (lx4.num[0] <= 0) {
						lx4.num.wait();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				lx4.num[0]--;
				if(Thread.currentThread().getName().equals("黄牛")){
					lx4.h++;
				}else {
					lx4.x++;
				}
				//System.out.println(Thread.currentThread().getName()+"消费了一件商品,剩余:" + lx4.num[0]);

			}
		}

	}
}
class ZhuangTai implements Runnable{
	@Override
	public void run(){

		try {
			while (true){
				Boolean b1  = lx4.map.get("s").isAlive();
				Boolean b2  = lx4.map.get("x").isAlive();
				if(b1==true){
					System.out.println("生产者线程状态为:存活");
				}else {
					System.out.println("生产者线程状态为:失活");
				}
				if (b2==true){
					System.out.println("消费者线程状态为:存活");
				}else {
					System.out.println("消费者线程状态为:失活");
				}
				Thread.sleep(10000);
				synchronized (lx4.num){
					lx4.num.notifyAll();
				}
			}
		}catch (Exception e){
			e.printStackTrace();
		}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值