Java多线程,消费问题

Java多线程,消费问题,下面代码问题在哪里
编写生产者消费者多线程程序,设有一个最大库存量为4的电视机仓库,生产10台电视机,一边生产一边销售(消费)。

public class Custom {
private int num = 0;
private int count;
public static void main(String[] args) {
Custom custom = new Custom();
Dc dc = custom.new Dc();
Ec ec = custom.new Ec();

	Thread thread = new Thread(dc);
	Thread thread1 = new Thread(ec);

	thread.setName("生产电视:");
	thread1.setName("销售1台电视,仓库还有:");

	thread.start();
	thread1.start();
}

class Dc implements Runnable {
	@Override
	public void run() {
		show();
	}

	private synchronized void show() {
		boolean flag = true;
		while (flag) {
			if (num > 3) {
				try {
					wait();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			num++;
			System.out.println(Thread.currentThread().getName() + num + "台");
			count++;
			if (count == 10) {
				flag = false;
			}
			notifyAll();
		}
	}

}

class Ec implements Runnable {
	private synchronized void show(){
		while (true) {
			if (num < 1) {
				try {
					this.wait();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			num--;
			System.out.println(Thread.currentThread().getName() + num + "台");
			notifyAll();
		}
	}

	@Override
	public void run() {
		show();
	}
}
}

Java多线程,消费

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值