【高并发面试题】写一个固定容量同步容器,拥有put和get方法,以及getCount方法, 能够支持2个生产者线程以及10个消费者线程的阻塞调用

方法一、wait()和notifyAll()

public class TestC1 {
    LinkedList<String> list = new LinkedList<>();
    final int MAX = 10;

    public int getCount(){
        return list.size();
    }

    public synchronized void put(String str){
        while (getCount() == MAX){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        list.add(str);
        System.out.println("生产===" + str);
        notifyAll();
    }

    public synchronized String get(){
        while (getCount() == 0){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        String t = list.getFirst();
        list.removeFirst();
        System.out.println("消费===" + t);
        notifyAll();
        return t;
    }

    public static void main(String[] args) {
        TestC1 testC1 = new TestC1();
        for (int i = 0; i < 2; i++) {
            new Thread(()->{
                for (int j = 0; j < 50; j++) {
                    testC1.put(Thread.currentThread().getName()+"==="+j);
                }
            }).start();
        }

        for (int i = 0; i < 10; i++) {
            new Thread(()->{
                for (int j = 0; j < 10; j++) {
                    testC1.get();
                }
            }).start();
        }
    }
}

方法二、ReentrantLock

public class TestC2 {
    volatile LinkedList<String> list = new LinkedList<>();
    final int MAX = 10;

    Lock lock = new ReentrantLock();
    Condition putLock = lock.newCondition();
    Condition getLock = lock.newCondition();

    public int getCount(){
        return list.size();
    }

    public void put(String str){
        lock.lock();
        try {
            while (getCount() == MAX){
                try {
                    putLock.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            list.add(str);
            System.out.println(Thread.currentThread().getName()+"生产===" + str);
            getLock.signalAll();
        }finally {
            lock.unlock();
        }
    }

    public String get(){
        lock.lock();
        try {
            while (getCount() == 0){
                try {
                    getLock.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            String str = list.getFirst();
            list.removeFirst();
            System.out.println(Thread.currentThread().getName()+"消费=======" + str);
            putLock.signalAll();
            return str;
        }finally {
            lock.unlock();
        }
    }

    public static void main(String[] args) {
        TestC2 testC1 = new TestC2();
        for (int i = 0; i < 2; i++) {
            new Thread(()->{
                for (int j = 0; j < 50; j++) {
                    testC1.put(Thread.currentThread().getName()+"==="+j);
                }
            }).start();
        }

        for (int i = 0; i < 10; i++) {
            new Thread(()->{
                for (int j = 0; j < 10; j++) {
                    testC1.get();
                }
            }).start();
        }
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值