23.异步模式-生产者、消费者

1.与保护性暂停GuardedObject不同,不需要产生结果与消费结果的线程一一对应。

2.消费队列可以用来平衡生产和消费的线程资源。

3.生产者负责产生结果数据,不关心数据该如何处理,而消费者专心处理结果数据。

4.消息队列是有容量限制的,满时不会再加入数据,空时不会再消耗数据。

5.jdk中各种阻塞队列,就是采用这种模式。

public class QueueDemo {

    public static void main(String[] args) {
        MessageQueue messageQueue = new MessageQueue(2);
        for (int i = 0; i < 3; i++) {
            int index = i;
            new Thread(() -> {
                Message msg = new Message(index, "msg"+index);
                messageQueue.put(msg);
            }, "生产者").start();
        }

        new Thread(() -> {
            while (true) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Message message = messageQueue.take();
                System.out.println("取出的message="+message);
            }

        }, "消费者").start();



    }

}

class MessageQueue {

    //双向队列
    private LinkedList<Message> linkedList = new LinkedList<>();
    //容量
    private int capcity;

    public MessageQueue(int capcity) {
        this.capcity = capcity;
    }

    /**
     * 取消息
     * @return
     */
    public Message take() {
        synchronized (linkedList) {
            while (linkedList.isEmpty()) {
                try {
                    System.out.println("队列为空,消费者等待");
                    linkedList.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            //返回头元素
            Message message = linkedList.removeFirst();
            System.out.println("取出一条消息:"+message);
            linkedList.notifyAll();
            return message;
        }
    }

    /**
     * 放入消息
     */
    public void put(Message message) {
        synchronized (linkedList) {
            while(linkedList.size() == capcity) {
                try {
                    System.out.println("队列已满,生产者等待");
                    linkedList.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            //添加到链表尾部
            linkedList.addLast(message);
            System.out.println("放入一条消息:"+message);
            //唤醒取消息线程
            linkedList.notifyAll();
        }
    }
}


final class Message {

    private int id;
    private Object value;

    public Message(int id, Object value) {
        this.id = id;
        this.value = value;
    }

    public int getId() {
        return id;
    }

    public Object getValue() {
        return value;
    }

    @Override
    public String toString() {
        return "Message{" +
                "id=" + id +
                ", value=" + value +
                '}';
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

卷土重来…

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值