第一步:创建实体对象
/**
* 商品实体类
*/
public class GoodsEntity {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
第二步:创建生产线程
/**
* 生产者线程
*/
public class ProducerThread implements Runnable{
public boolean isRuning=false;
GoodsEntity goodsEntity =null;//商品实体对象
int id=0; //商品id
ReentrantLock reentrantLock;
Condition proCondition;//生产线程的Condition
Condition consCondition; //消费线程的Condition
public ProducerThread(GoodsEntity goodsEntity,ReentrantLock reentrantLock,Condition proCondition,Condition consCondition){
this.goodsEntity=goodsEntity;
this.reentrantLock=reentrantLock;
this.proCondition=proCondition;
this.consCondition=consCondition;
}
@Override
public void run() {
while (isRuning) {
reentrantLock.lock();//同步锁
try {
Thread.sleep(100);
if (goodsEntity.getName() == null) { //name 为空说明已经消费完了需要生产商品
goodsEntity.setName("商品" + (++id));
Log.e("Thread_test_pro", Thread.currentThread().getName() + " 生产商品:---" + goodsEntity.getName());
//商品生产完成,通知消费线程消费
consCondition.signalAll();
} else {
//name 不为空说明商品还没有消费完,等待商品消费
proCondition.await();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
reentrantLock.unlock();//解锁
}
}
}
第三步:创建消费线程
/**
* 消费者线程
*/
public class ConsumerThread implements Runnable{
public boolean isRuning=false;
private GoodsEntity goodsEntity=null;
int count=0;
ReentrantLock reentrantLock;
Condition proCondition;//生产线程的Condition
Condition consCondition; //消费线程的Condition
public ConsumerThread(GoodsEntity goodsEntity, ReentrantLock reentrantLock,Condition proCondition, Condition consCondition){
this.goodsEntity=goodsEntity;
this.reentrantLock=reentrantLock;
this.proCondition=proCondition;
this.consCondition=consCondition;
}
@Override
public void run() {
while (isRuning){
reentrantLock.lock();
try {
if(goodsEntity.getName()==null){
//商品为空,说明没有生产新的商品,等待生产
consCondition.await();
}else{
Log.e("Thread_test_con", Thread.currentThread().getName()+ " 消费商品:" + goodsEntity.getName());
goodsEntity.setName(null);//清空商品
//商品消费完之后清空商品,通知生产
proCondition.signalAll();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
reentrantLock.unlock();
}
}
}
第四步:测试
public static void main(){
GoodsEntity goodsEntity=new GoodsEntity();
ReentrantLock reentrantLock=new ReentrantLock();
Condition proCondition=reentrantLock.newCondition();//生产线程的Condition
Condition consCondition=reentrantLock.newCondition(); //消费线程的Condition
ProducerThread producerThread=new ProducerThread(goodsEntity,reentrantLock,proCondition,consCondition);//生产者
ConsumerThread consumerThread=new ConsumerThread(goodsEntity,reentrantLock,proCondition,consCondition);//消费者
producerThread.isRuning=true;
consumerThread.isRuning=true;
Thread proThrad1=new Thread(producerThread,"生产线程1");
Thread proThrad2=new Thread(producerThread,"生产线程2");
Thread proThrad3=new Thread(producerThread,"生产线程3");
Thread conThrad1=new Thread(consumerThread,"消费线程1");
Thread conThrad2=new Thread(consumerThread,"消费线程2");
Thread conThrad3=new Thread(consumerThread,"消费线程3");
proThrad1.start();
proThrad2.start();
proThrad3.start();
conThrad1.start();
conThrad2.start();
conThrad3.start();
try {
Thread.sleep(6000);
producerThread.isRuning=false;
consumerThread.isRuning=false;
Log.e("Thread_test", "proThrad1.isAlive()="+proThrad1.isAlive());
Thread.sleep(2000);
Log.e("Thread_test", "proThrad1.isAlive()2="+proThrad1.isAlive());
} catch (InterruptedException e) {
Log.e("Thread_test", "InterruptedException ex="+e.toString());
e.printStackTrace();
}
}
备注: signalAll()调用之后会自动退出同步锁