最近学习JAVA多线程,借鉴他人尝试了一个供销社模型,模拟消费者生产者问题。
代码:
{CSDN:CODE:348719}
实现结果:
Try to consume empty storage!Fail to consume!
Produce 20 production(s),current production storage is 20
Consume 20 production(s),current production storage is 0
Try to consume empty storage!Fail to consume!
Produce 10 production(s),current production storage is 10
Consume 10 production(s),current production storage is 0
Try to consume empty storage!Fail to consume!
Produce 30 production(s),current production storage is 30
Produce 40 production(s),current production storage is 70
Consume 40 production(s),current production storage is 30
Consume 30 production(s),current production storage is 0
Produce 20 production(s),current production storage is 20
Consume 20 production(s),current production storage is 0
Try to consume empty storage!Fail to consume!
Produce 10 production(s),current production storage is 10
Consume 10 production(s),current production storage is 0
Try to consume empty storage!Fail to consume!
Produce 30 production(s),current production storage is 30
Produce 40 production(s),current production storage is 70
Consume 40 production(s),current production storage is 30
Consume 30 production(s),current production storage is 0