生产者与消费者问题

  • wait()和notify()&notifyAll()必须工作在加锁(synchronized)的代码块中。
  • 抢占的锁和释放的锁要是同一对象的锁,否则抛出IllegalMonitorStateException。

线程状态间的转换

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Stack.java

package thread;

public class Stack {
	private Object[] objs;
	private int index = 0;
    
	public Stack() {
		this(3);
	}

	public Stack(int length) {
		this.objs = new Object[length];
	}

	public void push(Object obj) {
		synchronized (this) {
			while (index == this.objs.length) {
				try {
					this.wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			for (int i = 0; i < 100000; i++)	{};
			objs[index] = obj;
			index++;
			this.notifyAll();
		}
	}

	public Object pop() {
		synchronized (this) {
			while (index == 0) {
				try {
					this.wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			--index;
			for (int i = 0; i < 100000; i++)	{};
			Object value = this.objs[index];
			this.notifyAll();
			return value;
		}
	}
}

 

Producer.java

package thread;

import java.util.Random;

public class Producer extends Thread{
	private Stack stack;
	
	public Producer(Stack stack){
		this.stack = stack;
	}
	
	@Override
	public void run() {
		Random ran = new Random();
		for (int i = 0; i < 20; i++) {
			Object obj = ran.nextInt(200);
			stack.push(obj);
			System.out.println(obj+" 压入成功。" );
			try {
				Thread.sleep(300);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}

 

Customer.java

package thread;

public class Customer extends Thread{
	private Stack stack;

	public Customer(Stack stack) {
		this.stack = stack;
	}

	@Override
	public void run() {
		for (int i = 0; i < 20; i++) {
			Object value = stack.pop();
			System.out.println(value+" 弹出成功。");
			try {
				Thread.sleep(300);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}

 

Test.java

package thread;

public class Test {
	public static void main(String[] args) {
		Stack stack = new Stack();
		for (int i = 0; i < 20; i++) {
			Producer p = new Producer(stack);
			p.setName("producerThread");
			p.start();
		}
		for (int i = 0; i < 20; i++) {
			Customer c = new Customer(stack);
			c.setName("customerThread");
			c.start();
		}
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值