生产者消费者(消费者要消费完才能退出)

package productAndConsummer;

import java.util.ArrayList;

public class Main {

public static void main(String[] args) throws InterruptedException {
Resource res = new Resource();

ArrayList<Thread> productList= new ArrayList<Thread>();
ArrayList<Thread> conSummerList= new ArrayList<Thread>();

for(int i=0;i<5;i++){
Thread pro = new Thread(new Product(res),"Product-"+i);
productList.add(pro);
res.addProduct(pro);
pro.start();
}

for(int i=0;i<5;i++){
Thread con = new Thread(new ConSummer(res),"ConSummer-"+i);
conSummerList.add(con);
con.start();
}

//一秒后中断所有的消费线程和生产者线程
Thread.sleep(1000);


Thread.sleep(1000);
for(Thread t : productList){
t.interrupt();
}
for(Thread t : conSummerList){
t.interrupt();
}

for(Thread t : productList){
t.join();
}
for(Thread t : conSummerList){
t.join();
}


System.out.println("Main 退出");
}

}

class Product implements Runnable{
private Resource res ;
public Product(Resource res){
this.res = res;

}

@Override
public void run() {
while(!Thread.interrupted()){

res.create();

}

System.out.println(Thread.currentThread().getName()+"-----------生产者线程结束----------");
}

}

class ConSummer implements Runnable{
private Resource res ;
private volatile boolean needStopStrong = false ;
public ConSummer(Resource res){
this.res = res;

}
@Override
public void run() {

int r = Resource.NEED_STOP;
do{
r = res.consume();
if(r==Resource.BECAUSE_PRODUCT_NTOSTOP_NEED_CONTINUE){
boolean isIr = Thread.interrupted();
try {
Thread.sleep((int) (Math.random()*100));
} catch (InterruptedException e) {

}
if(isIr){
Thread.currentThread().interrupt();
}
}

}while(r!=Resource.NEED_STOP && !needStopStrong);

System.out.println(Thread.currentThread().getName()+"-----------消费者线程结束----------");
}

}

 

 

package productAndConsummer;

import java.util.ArrayList;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Resource {
public static final int NEED_STOP = -1;
public static final int NEED_CONTINUE = 1;
public static final int BECAUSE_PRODUCT_NTOSTOP_NEED_CONTINUE = 2;

private boolean hasR = false;
private int num = 0;
private Lock lock = new ReentrantLock();
private Condition not_True = lock.newCondition(); //消费者如果发现资源非 True 。则要阻塞在该条件下,等待生产者线程唤醒
private Condition not_False = lock.newCondition(); //生产者如果发现资源非 False。则要阻塞在该条件下,等待消费者线程唤醒
private ArrayList<Thread> listP= new ArrayList<Thread>();
private ArrayList<Thread> listC= new ArrayList<Thread>();

public void addProduct(Thread p) {
listP.add(p);
}

public void addConsume(Thread c) {
listC.add(c);
}

public void create() {

try{
lock.lock();

while(hasR){

try {
not_False.await();
} catch (InterruptedException e) {
System.out.println(Thread.currentThread().getName()+"+++生产线程从等待中被中断---");
Thread.currentThread().interrupt();
return;
}


}



num += 1;
System.out.println(Thread.currentThread().getName()+"生产者生产第:"+num+"个......");

hasR = true;
not_True.signal();
}finally{
lock.unlock();
}

}

public int consume() {

try{
lock.lock();

while(!hasR ){

try {
not_True.await();
} catch (InterruptedException e) {
System.out.println(Thread.currentThread().getName()+"+++消费线程从等待中被中断---");
Thread.currentThread().interrupt();
break;
}

}



if(!hasR){

boolean isAlive = false;

for(Thread t : listP){
if(t.isAlive()){
isAlive = true;
}
}

if(isAlive){
System.out.println("--------------------------------因为生产者还没有退出,所以返回true");
return Resource.BECAUSE_PRODUCT_NTOSTOP_NEED_CONTINUE;
}


System.out.println(Thread.currentThread().getName()+"消费线程判断不需要消费");
return Resource.NEED_STOP;
}


System.out.println(Thread.currentThread().getName()+"消费者消费第:"+num+"个......");

hasR = false;
not_False.signal();
return Resource.NEED_CONTINUE;
}finally{
lock.unlock();
}

}
}

posted on 2018-07-29 15:16 卧似长剑 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/chencanjian/p/9385413.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值