生产者消费者模型

面试时要求写一个生产者消费者模型,特在此记录一下

给对象加锁:

package newthread;

import java.util.*;

class Counter{
    List<Integer> lists = new ArrayList<>();
    int size = 10;
    Object object = new Object();

    public void add() throws InterruptedException {
            synchronized (object){
                while (lists.size() >= size){
                    System.out.println("队列满了");
                    object.wait();
                }
                lists.add(1);
                System.out.println(Thread.currentThread().getName() + " 加入了数据, 当前列表大小为 " + lists.size());
                object.notifyAll();
            }
    }

    public void del() throws InterruptedException {
            synchronized (object){
                while (lists.size() == 0){
                    System.out.println("队列空了");
                    object.wait();
                }
                lists.remove(0);
                System.out.println(Thread.currentThread().getName() + " 获取了数据, 当前列表大小为 " + lists.size());
                object.notifyAll();
            }
    }
}

class Provider implements Runnable {

    Counter counter;

    public Provider(Counter counter){
        this.counter = counter;
    }
    @Override
    public void run() {
        while (true) {
            try {
                counter.add();
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}


class Consumer implements Runnable{

    Counter counter;
    public Consumer(Counter counter){
        this.counter = counter;
    }
    @Override
    public void run() {
       while (true){
           try {
               counter.del();
               Thread.sleep(100);
           } catch (InterruptedException e) {
               e.printStackTrace();
           }
       }
    }
}

public class ProviderConsumer{
    public static void main(String[] args) {
        Counter counter = new Counter();
        Provider provider = new Provider(counter);
        Consumer consumer = new Consumer(counter);
        Thread provider1 = new Thread(provider, "provider1");
        Thread provider2 = new Thread(provider, "provider2");
        Thread consumer1 = new Thread(consumer, "consumer1");
        Thread consumer2 = new Thread(consumer, "consumer2");
        provider1.start();
        provider2.start();
        consumer1.start();
        consumer2.start();

    }
}

给方法加锁

import java.util.ArrayList;
import java.util.List;

class Counter {
    List<Integer> lists = new ArrayList<>();
    int size = 10;

    public synchronized void add() throws InterruptedException {

        while (lists.size() >= size) {
            System.out.println("队列满了");
            wait();
        }
        lists.add(1);
        System.out.println(Thread.currentThread().getName() + " 加入了数据, 当前列表大小为 " + lists.size());
        notifyAll();

    }

    public synchronized void del() throws InterruptedException {
        while (lists.size() == 0) {
            System.out.println("队列空了");
            wait();
        }
        lists.remove(0);
        System.out.println(Thread.currentThread().getName() + " 获取了数据, 当前列表大小为 " + lists.size());
        notifyAll();
    }
}

class Provider implements Runnable {

    Counter counter;

    public Provider(Counter counter) {
        this.counter = counter;
    }

    @Override
    public void run() {
        while (true) {
            try {
                counter.add();
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}


class Consumer implements Runnable {

    Counter counter;

    public Consumer(Counter counter) {
        this.counter = counter;
    }

    @Override
    public void run() {
        while (true) {
            try {
                counter.del();
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

public class ProviderConsumer {
    public static void main(String[] args) {
        Counter counter = new Counter();
        Provider provider = new Provider(counter);
        Consumer consumer = new Consumer(counter);
        Thread provider1 = new Thread(provider, "provider1");
        Thread provider2 = new Thread(provider, "provider2");
        Thread consumer1 = new Thread(consumer, "consumer1");
        Thread consumer2 = new Thread(consumer, "consumer2");
        provider1.start();
        provider2.start();
        consumer1.start();
        consumer2.start();

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值