多线程----生产者与消费者问题

生产者与消费者问题

蛋糕店

package com.bennett.test1014;

/**
 * 消费者与生产者问题
 * 
 * @author bennett
 *
 */
public class ProductAndConstmerTest {
//	2.定义消费产品
	static class Cake {
//		定义蛋糕下标
		private int index;

		public Cake(int index) {
			super();
			this.index = index;
		}

		@Override
		public String toString() {
			return "Cake [index=" + index + "]";
		}
	}

//	3.定义蛋糕店
	static class CakeShop {
//		蛋糕的生产最高数量
		final int COUNT = 5;
//		定义存储蛋糕的容器
		private Cake[] cakes = new Cake[COUNT];
		private int index = 0;
//		开始生产蛋糕
		public synchronized void push(Cake cake) throws InterruptedException {
//			判断何时停止生产蛋糕
			System.out.println("开始生产蛋糕了"+cake);
			if (index >= 5) {
				this.wait();
			}
			cakes[index++] = cake;
//			唤醒消费者线程,可以开始消费了
			this.notifyAll();
		}
//		停止生产蛋糕
		public synchronized Cake pop() throws InterruptedException {
//			停止生产蛋糕的判断条件
			if (index == 0) {
				this.wait();
			}
			Cake cake = cakes[--index];
			System.out.println("销售蛋糕了"+cake);
//			唤醒生产者线程,可以开始生产了
			this.notifyAll();
			return cake;
		}
	}

//	1.定义两个线程
//	1.1.生产者
	static class Product extends Thread {
//		定义蛋糕店对象
		private CakeShop cakeshop;
		public Product(CakeShop cakeshop) {
			super();
			this.cakeshop = cakeshop;
		}
		@Override
		public void run() {
//			生产10次
			for (int i = 0; i < 10; i++) {
				Cake cake = new Cake(i);
				try {
					cakeshop.push(cake);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
//	1.2.消费者
	static class Constomer extends Thread {
//		定义蛋糕店对象
		private CakeShop cakeshop;
		public Constomer(CakeShop cakeshop) {
			super();
			this.cakeshop = cakeshop;
		}
		@Override
		public void run() {
//			消费十次
			for (int i = 0; i < 10; i++) {
				try {
					cakeshop.pop();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public static void main(String[] args) {
		CakeShop cakeshop = new CakeShop();
		new Product(cakeshop).start();
		new Constomer(cakeshop).start();
	}
}

运行结果:

开始生产蛋糕了Cake [index=0]
开始生产蛋糕了Cake [index=1]
开始生产蛋糕了Cake [index=2]
开始生产蛋糕了Cake [index=3]
开始生产蛋糕了Cake [index=4]
开始生产蛋糕了Cake [index=5]
销售蛋糕了Cake [index=4]
销售蛋糕了Cake [index=3]
销售蛋糕了Cake [index=2]
销售蛋糕了Cake [index=1]
销售蛋糕了Cake [index=0]
开始生产蛋糕了Cake [index=6]
开始生产蛋糕了Cake [index=7]
开始生产蛋糕了Cake [index=8]
开始生产蛋糕了Cake [index=9]
销售蛋糕了Cake [index=9]
销售蛋糕了Cake [index=8]
销售蛋糕了Cake [index=7]
销售蛋糕了Cake [index=6]
销售蛋糕了Cake [index=5]

售票系统

package com.bennett.test1024;

public class ProductAndCustomerSys {
//	票
	static class Ticket{
		int id;
		String departure;
		String start;
		String end;
		
		public Ticket() {
			super();
		}
		public Ticket(int id) {
			super();
			this.id = id;
		}
		public Ticket(int id, String departure, String start, String end) {
			super();
			this.id = id;
			this.departure = departure;
			this.start = start;
			this.end = end;
		}
		@Override
		public String toString() {
			return "Ticket [id=" + id + ", departure=" + departure + ", start=" + start + ", end=" + end + "]";
		}
	}
//	出票机
	static class ChuPiaoJi{
//		定义票的对大数量
		final int COUNT = 8;
//		定义存储票的容器
		private Ticket[] tickets = new Ticket[COUNT];
		private int index = 0;
//		开始生产票
		
		public synchronized void push(Ticket ticket) throws InterruptedException {
			System.out.println("开始生产票"+ticket);
			
			if (index > 8) {
				this.wait();
			}
			tickets[index++] = ticket;
			this.notifyAll();
		}
//		开始生产票
		public synchronized Ticket pop() throws InterruptedException {
			if (index == 0) {
				this.wait();
			}
			Ticket ticket = tickets[--index];
			System.out.println("开始销售票"+ticket);
			
			this.notifyAll();
			return ticket;
		}
	}
//	定义两个线程
//	生产票线程
	static class Product extends Thread{
		private ChuPiaoJi chuPiaoJi;
		
		public Product(ChuPiaoJi chuPiaoJi) {
			super();
			this.chuPiaoJi = chuPiaoJi;
		}
		@Override	
		public void run() {
			for (int i = 0; i < 8; i++) {
				Ticket ticket = new Ticket(i, "T1234", "西安", "兰州");
				try {
					chuPiaoJi.push(ticket);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
//	销售票线程
	static class Customer extends Thread{
		private ChuPiaoJi chuPiaoJi;
		public Customer(ChuPiaoJi chuPiaoJi) {
			super();
			this.chuPiaoJi = chuPiaoJi;
		}
		
		@Override
		public void run() {
			for (int i = 0; i < 8; i++) {
				try {
					chuPiaoJi.pop();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
//	测试类
	public static void main(String[] args) {
		ChuPiaoJi chuPiaoJi = new ChuPiaoJi();
		new Product(chuPiaoJi).start();
		new Customer(chuPiaoJi).start();
	}
}

运行结果:

开始生产票Ticket [id=0, departure=T1234, start=西安, end=兰州]
开始生产票Ticket [id=1, departure=T1234, start=西安, end=兰州]
开始生产票Ticket [id=2, departure=T1234, start=西安, end=兰州]
开始生产票Ticket [id=3, departure=T1234, start=西安, end=兰州]
开始生产票Ticket [id=4, departure=T1234, start=西安, end=兰州]
开始生产票Ticket [id=5, departure=T1234, start=西安, end=兰州]
开始生产票Ticket [id=6, departure=T1234, start=西安, end=兰州]
开始生产票Ticket [id=7, departure=T1234, start=西安, end=兰州]
开始销售票Ticket [id=7, departure=T1234, start=西安, end=兰州]
开始销售票Ticket [id=6, departure=T1234, start=西安, end=兰州]
开始销售票Ticket [id=5, departure=T1234, start=西安, end=兰州]
开始销售票Ticket [id=4, departure=T1234, start=西安, end=兰州]
开始销售票Ticket [id=3, departure=T1234, start=西安, end=兰州]
开始销售票Ticket [id=2, departure=T1234, start=西安, end=兰州]
开始销售票Ticket [id=1, departure=T1234, start=西安, end=兰州]
开始销售票Ticket [id=0, departure=T1234, start=西安, end=兰州]
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值