商城类
public class Tmail {
//产品数量
private int count;
//产品的最大容量
public final int MAX_COUNT=10;
//消费者生产产品
public synchronized void push(){
while(count>=MAX_COUNT){
try {
System.out.println(Thread.currentThread().getName()+" 库存数量达到上限,生产者停止生产");
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
count++;
System.out.println(Thread.currentThread().getName()+" 生产者生产,当前库存为:"+count);
notifyAll();
}
//消费者消费产品
public synchronized void take(){
while(count<=0){
try {
System.out.println(Thread.currentThread().getName()+" 库存数量为0,消费者停止消费");
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
count--;
System.out.println(Thread.currentThread().getName()+" 消费者消费,当前库存为:"+count);
notifyAll();
}
}
以上商城类,表明商品数量count和最大商品数量。
当生产的商品达到最大商品数量后,生产者将停止生产。
当消费的商品为零后,消费者将停止消费。
当生产者开始生产,唤醒所有正在等待的消费者。
当消费者开始消费,唤醒所有正在等待的生产者。
使用while修饰wait
以上涉及到了while 和 if 修饰wait的问题
如果使用 if 修饰wait 那么线程被唤醒后,将继续执行wait 后面的内容,不会再次进行判断。
如果使用while修饰,那么线程被唤醒后,将会继续判断是否满足条件
生产者类
class PushTarget implements Runnable{
private Tmail tmail;
public PushTarget(Tmail tmail) {
this.tmail = tmail;
}
@Override
public void run() {
while(true){
tmail.push();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
消费者类
class takeTarget implements Runnable{
private Tmail tmail;
public takeTarget(Tmail tmail) {
this.tmail = tmail;
}
@Override
public void run() {
while(true){
tmail.take();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
测试类
public class MainTest {
public static void main(String[] args) {
Tmail tmail=new Tmail();
PushTarget pushTarget=new PushTarget(tmail);
takeTarget takeTarget=new takeTarget(tmail);
new Thread(pushTarget).start();
new Thread(pushTarget).start();
new Thread(takeTarget).start();
}
}