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

package productAndConsummer;java

import java.util.ArrayList;dom

public class Main {ide

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 退出");
}this

}线程

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() {get

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);it

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

}io

 

 

package productAndConsummer;class

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(); } }}

相关文章
相关标签/搜索