package cn.itheima.thread;
import java.util.concurrent.ExecutorService;
public class HelloThread {
public static void main(String[] args) {
ProductFactory mFactory = new ProductFactory();
new Thread(new Task1(mFactory)).start();
new Thread(new Task2(mFactory)).start();
}
}
// shengchan thread
class Task1 implements Runnable{
public ProductFactory mFactory;
public Task1(ProductFactory mFactory){
this.mFactory = mFactory;
}
@Override
public void run() {
while(true){
mFactory.product();
}
}
}
// xiaofei thread
class Task2 implements Runnable{
public ProductFactory mFactory;
public Task2(ProductFactory mFactory){
this.mFactory = mFactory;
}
@Override
public void run() {
while(true){
mFactory.consume();
}
}
}
class ProductFactory {
int total = 0;
// shengchan
public synchronized void product(){
try{
if(total>=20){
System.out.pr
生产消费问题(线程)
最新推荐文章于 2021-12-04 17:51:29 发布