公司真题1

用wait和notifyAll实现生产者消费者

import java.util.*;

public class Main {

    static int size = 5;

    static List<Integer> list = new ArrayList<>();

    static class Consumer implements Runnable {

        @Override
        public void run() {
            while (true) {
                synchronized (list) {

                    if (list.size() == size) {

                        System.out.println("size=full");

                        try {
                            list.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }

                    list.add(0);

                    System.out.println(list.size());

                    list.notifyAll();
                }
            }
        }
    }

    static class Producer implements Runnable {

        @Override
        public void run() {
            while (true) {
                synchronized (list) {

                    if (list.size() == 0) {

                        System.out.println("size=0");

                        try {
                            list.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }

                    list.remove(0);

                    System.out.println(list.size());

                    list.notifyAll();
                }
            }
        }
    }

    public static void main(String[] args) {
        new Thread(new Consumer()).start();
        new Thread(new Producer()).start();
    }

}

用ReentrantLock和Condition 实现生产者消费者

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Main {

    static Lock lock = new ReentrantLock();
    static Condition condition = lock.newCondition();

    static int size = 5;
    static List<Integer> list = new ArrayList<>();

    static class Consumer implements Runnable {

        @Override
        public void run() {

            while (true) {
                lock.lock();

                if (list.size() == 0) {
                    try {
                        condition.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                list.remove(0);

                System.out.println(list.size());

                condition.signalAll();

                lock.unlock();
            }

        }
    }

    static class Producer implements Runnable {

        @Override
        public void run() {

            while (true) {
                lock.lock();

                if (list.size() == size) {
                    try {
                        condition.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                list.add(0);

                System.out.println(list.size());

                condition.signalAll();

                lock.unlock();
            }


        }
    }

    public static void main(String[] args) {
        new Thread(new Consumer()).start();
        new Thread(new Producer()).start();
    }
}

数组实现队列

public class MyQueue<T> {
    private Object[] array;
    private int size;
    private int head, last;

    public MyQueue() {
        array = new Object[10];
        head = 0;
        last = 0;
        size = 0;
    }

    public MyQueue(int length) {
        array = new Object[length];
        head = 0;
        last = 0;
        size = 0;
    }

    public T pop() {
        if (isEmpty()) {
            return null;
        }

        T result = (T) array[head];
        head = (head + 1) % array.length;
        size--;

        return result;
    }

    public void push(T t) {
        if (isFull()) {
            return;
        }

        array[last] = t;
        last = (last + 1) % array.length;
        size++;
    }

    public Object[] getArray() {
        return array;
    }

    public int getSize() {
        return size;
    }

    public int getHead() {
        return head;
    }

    public int getLast() {
        return last;
    }

    public boolean isFull() {
        return array.length == size;
    }

    public boolean isEmpty() {
        return size == 0;
    }
}

实现阻塞队列

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class MyBlockingQueue<T> extends MyQueue {
    private Lock lock = new ReentrantLock();
    private Condition full = lock.newCondition();
    private Condition empty = lock.newCondition();

    public MyBlockingQueue() {
    }

    public MyBlockingQueue(int length) {
        super(length);
    }

    @Override
    public T pop() {

        lock.lock();

        try {
            if (isEmpty()) {
                System.out.println("空");

                empty.await();

                System.out.println("已存放");
            }

            full.signal();

            return (T) super.pop();

        } catch (InterruptedException e) {
            e.printStackTrace();
            return null;
        } finally {
            lock.unlock();
        }
    }

    @Override
    public void push(Object o) {

        lock.lock();

        try {
            if (isFull()) {
                System.out.println("满");

                full.await();

                System.out.println("已消费");
            }

            empty.signal();

            super.push(o);

        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }

    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值