c++一个线程发消息到另外一个线程_手写一个简单的线程池

阻塞队列

首先我们造一个阻塞队列,用来存放任务,当然你不想造直接用jdk的也行

阻塞队列,需要一个队列,还需要一个让他阻塞的东西(条件),我们使用ReentrantLock和他的小伙伴Condition,那么阻塞队列的成员变量如下

//容量
    private int capacity;

    public MyQueue(int capacity) {
    
        this.capacity = capacity;
        this.queue = new ArrayDeque<>(capacity);
    }
    //存放元素的队列
    private Deque<T> queue;

    private ReentrantLock lock = new ReentrantLock();

    //等待不为满的条件变量
    private Condition notFull = lock.newCondition();
    //等待不为空的条件变量
    private Condition notEmpty = lock.newCondition();

接下来写他的读方法

非阻塞方法get()

//非阻塞方法get
    public T get() {
    
        lock.lock();
        try {
    
            if (queue.isEmpty()) return null;
            T res = queue.removeLast();
            //不为满了,唤醒等待插入的线程
            notFull.signal();
            return res;
        } finally {
    
            lock.unlock();
        }
    }

阻塞等待take()

//阻塞等待
    public T take() {
    
        lock.lock();
        try {
    
            while (queue.isEmpty()) {
    
                log.info("队列为空");
                try {
    
                    //等待不为空
                    notEmpty.await();
                } catch (InterruptedException e) {
    
                    e.printStackTrace();
                }
            }
            T res = queue.removeLast();
            log.info("获取成功: " + res);
            //不为满了,唤醒等待插入的线程
            notFull.signal();
            return res;
        } finally {
    
            lock.unlock();
        }
    }

限时阻塞等待take(long time, TimeUnit unit)

//阻塞等待
    public T take() {
    
        lock.lock();
        try {
    
            while (queue.isEmpty()) {
    
                log.info("队列为空");
                try {
    
                    //等待不为空
                    notEmpty.await();
                } catch (InterruptedException e) {
    
                    e.printStackTrace();
                }
            }
            T res = queue.removeLast();
            log.info("获取成功: " + res);
            //不为满了,唤醒等待插入的线程
            notFull.signal();
            return res;
        } finally {
    
            lock.unlock();
        }
    }

以下是写方法

非阻塞插入offer(T t)

//非阻塞插入
    public boolean offer(T t) {
    
        lock.lock();
        try {
    
            if (queue.size() == capacity) return false;
            queue.addLast(t);
            //队列不为空了 唤醒
            notEmpty.signal();
            return true;
        } finally {
    
            lock.unlock();
        }
    }

阻塞插入put(T t)

//阻塞插入
    public void put(T t) {
    
        lock.lock();
        try {
    
            while (queue.size() == capacity) {
    
                log.info("队列为满");
                try {
    
                    //队列满了,等待
                    notFull.await();
                } catch (InterruptedException e) {
    
                    e.printStackTrace();
                }
            }
            queue.addLast(t);
            log.info("插入成功: " + t);
            //队列不为空了 唤醒
            notEmpty.signal();
        } finally {
    
            lock.unlock();
        }
    }

限时阻塞插入put(T t, long time, TimeUnit unit)

//阻塞限时插入
    public boolean put(T t, long time, TimeUnit unit) {
    
        lock.lock();
        try {
    
            long wait = unit.toNanos(time);
            while (queue.size() == capacity) {
    
                if (wait <= 0) {
    
                    log.info("插入超时: " + t);
                    return false;
                }
                //等待不满
                try {
    
                    wait = notFull.awaitNanos(wait);
                } catch (InterruptedException e) {
    
                    e.printStackTrace();
                }
            }
            queue.addLast(t);
            log.info("插入成功: " + t);
            //队列不为空了 唤醒
            notEmpty.signal();
            return true;
        } finally {
    
            lock.unlock();
        }
    }

撸完了测试下代码写的对不对

MyQueue<Integer> myQueue = new MyQueue<>(2);
        for (int i = 1; i <= 10; i++) {
    
            int j = i;
            new Thread(() -> {
    
              
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值