@(一只心中无码的程序员)专栏

叨叨两句

  1. 果然,释放天性后,果然强悍的不行啊,哈哈!

生产者消费者模型相关案例(案例1-2)

案例1:队列中存基本数据类型,轮流输出

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class Test {
    public static void main(String[] args) {
        //1.创建缓冲区
        BlockingQueue<Integer> queue = new ArrayBlockingQueue<>(5);
        //2.创建生产者对象
        Producer producer = new Producer(queue);
        //3.创建消费者者对象
        Consumer consumer = new Consumer(queue);

        producer.start();
        consumer.start();
    }
}

import java.util.concurrent.BlockingQueue;

//代表生产者
public class Producer extends Thread {
    private BlockingQueue<Integer> queue;
    private int num;
    public Producer(BlockingQueue<Integer> queue) {
        this.queue = queue;
    }

    @Override
    public void run() {
        while(true){
            try {
                queue.put(num);
                System.out.println("我生产了数据"+num);
                num++;
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        
    }
}

import java.util.concurrent.BlockingQueue;

//代表消费者
public class Consumer extends Thread {
    private BlockingQueue<Integer> queue;
    public Consumer(BlockingQueue<Integer> queue) {
        this.queue = queue;
    }

    @Override
    public void run() {
        while(true){
            try {
                int num = queue.take();
                System.out.println("我消费了数据"+num);
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

案例2:队列中存对象,轮流输出

//代表消费者
public class Consumer extends Thread {
    private Basket basket;
    
    public Consumer(Basket basket) {
        this.basket = basket;
    }

    @Override
    public void run() {
        while(true) {
            if(basket.getQueue().size() == 0) {
                System.out.println("篮子空了,取不了了");
            }
            Apple apple = basket.getApple();
            System.out.println("我拿出了苹果" + apple.getName());
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}



//代表生产者
public class Producer extends Thread {
    private Basket basket;
    private int num;
    
    public Producer(Basket basket) {
        super();
        this.basket = basket;
    }

    @Override
    public void run() {
        while(true) {
            if(basket.getQueue().size() == 3) {
                System.out.println("篮子放满了,放不进去了");
            }
            Apple apple = new Apple("name" + num);
            basket.putApple(apple);
            System.out.println("我放入了苹果" + apple.getName());
            num++;
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

public class Apple {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Apple() {
        
    }

    public Apple(String name) {
        this.name = name;
    }

        
    
}

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class Basket {
    private BlockingQueue<Apple> queue = new ArrayBlockingQueue<>(3);
    
    public void putApple(Apple apple) {
        try {
            queue.put(apple);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    
    public Apple getApple() {
        try {
            Apple apple;
            apple = queue.take();
            return apple;
        } catch (InterruptedException e) {
            return null;
        }
    }

    public BlockingQueue<Apple> getQueue() {
        return queue;
    }
    
}

public class Test {
    public static void main(String[] args) {
        //1.创建缓冲区
        Basket basket = new Basket();
        //2.创建生产者对象
        Producer producer = new Producer(basket);
        //3.创建消费者者对象
        Consumer consumer = new Consumer(basket);
        
        producer.start();
        consumer.start();
    }
}


Wall_Breaker
2.1k 声望1.2k 粉丝

生死之间,就是我的跃迁之路,全程记录,欢迎见证