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