zoukankan      html  css  js  c++  java
  • 生产者消费者(消费者要消费完才能退出)

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

    }
    }

  • 相关阅读:
    算法的学习 — 冒泡排序
    自定义UICollectionLayout布局 —— UIKit之学习UICollectionView记录一《瀑布流》
    HDU 1541 Stars (线段树||树状数组)
    HDU 1617 Phone List (排序||字典树)
    CSU 1312 CX and girls (最短路)
    CSU 1320 Scoop water (卡特兰数)
    POJ 1838 Banana (并查集)
    POJ 1837 Balance (DP)
    POJ 1088 滑雪 (记忆化搜索)
    TYVJ 1261 可达总数 (BFS)
  • 原文地址:https://www.cnblogs.com/chencanjian/p/9385413.html
Copyright © 2011-2022 走看看