生產者消費者(消費者要消費完才能退出)

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

相關文章
相關標籤/搜索