**# java多线程等待唤醒机制,生产者消费者问题
涉及的类:
包子铺类线程,消费者线程,包子类,测试类。
包子铺线程:
import java.util.Random;
/**
* @ClassName
>
- [ ]
- ~~这里是引用~~
: SteamedBunRestaurant
* @Description 本类功能:包子铺线程,生产包子
* @Author lzl
* @Date 2020.10.03
* @Time 20:24
*/
public class SteamedBunRestaurant implements Runnable {
private BaoZi baoZi;
@Override
public void run ( ) {
while ( true ) {
synchronized ( baoZi ) {
if ( baoZi.isStatus () == false ) {
String filling;
System.out.println ( "生产一个" + ( filling = new Random ().nextInt ( 2 ) > 0 ? "猪肉白菜" : "蛋黄" ) + "馅的包子" );
baoZi.setFilling ( filling );
try {
System.out.println ( "等待5秒生产包子........" );
Thread.sleep ( 5000 );
} catch ( InterruptedException e ) {
e.printStackTrace ();
}
System.out.println ( "生产完成,重设包子状态,唤醒消费者线程=========" );
baoZi.setStatus ( true );
baoZi.notify ();
} else {
try {
baoZi.wait ();
} catch ( InterruptedException e ) {
e.printStackTrace ();
}
}
}
}
}
public SteamedBunRestaurant ( BaoZi baoZi ) {
this.baoZi = baoZi;
}
public SteamedBunRestaurant ( ) {
}
public BaoZi getBaoZi ( ) {
return baoZi;
}
public void setBaoZi ( BaoZi baoZi ) {
this.baoZi = baoZi;
}
}
消费者线程,用于吃包子:
/**
* @ClassName: Consumers
* @Description 本类功能:消费者线程,用于吃包子
* @Author lzl
* @Date 2020.10.03
* @Time 19:56
*/
public class Consumers implements Runnable {
private BaoZi baoZi;
@Override
public void run ( ) {
while ( true ) {
synchronized ( baoZi ) {
if ( baoZi.isStatus () == true ) {
System.out.println ( "包子做好了,吃掉" + baoZi.getFilling () + "馅的包子" );
try {
System.out.println ( "等待5秒吃掉包子........" );
Thread.sleep ( 5000 );
} catch ( InterruptedException e ) {
e.printStackTrace ();
}
System.out.println ( "吃掉包子,重设包子状态,唤醒包子铺线程-----------------" );
System.out.println ( "==================================================" );
System.out.println ();
System.out.println ();
baoZi.setStatus ( false );
baoZi.notify ();
} else {
try {
baoZi.wait ();
} catch ( InterruptedException e ) {
e.printStackTrace ();
}
}
}
}
}
public Consumers ( BaoZi baoZi ) {
this.baoZi = baoZi;
}
public Consumers ( ) {
}
public BaoZi getBaoZi ( ) {
return baoZi;
}
public void setBaoZi ( BaoZi baoZi ) {
this.baoZi = baoZi;
}
}
包子类,保存包子的状态和馅,状态status,false为有包子,true为没有包子
/**
* @ClassName: BaoZi
* @Description 本类功能:包子类,储存包子状态和馅
* @Author lzl
* @Date 2020.10.03
* @Time 20:24
*/
public class BaoZi {
private boolean status=false;
private String filling;
public BaoZi ( boolean status , String filling ) {
this.status = status;
this.filling = filling;
}
public boolean isStatus ( ) {
return status;
}
public void setStatus ( boolean status ) {
this.status = status;
}
public String getFilling ( ) {
return filling;
}
public void setFilling ( String filling ) {
this.filling = filling;
}
public BaoZi ( ) {
}
}
测试类,实现生产者消费者问题:
public class TestBaoZi {
public static void main ( String[] args ) {
BaoZi baoZi = new BaoZi ();
SteamedBunRestaurant steamedBunRestaurant = new SteamedBunRestaurant ( baoZi );
Consumers consumers = new Consumers ( baoZi );
Thread producers = new Thread ( steamedBunRestaurant );
Thread customer = new Thread ( consumers );
producers.start ();
customer.start ();
}
}
程序截图: