十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
//消息队列类,Java线程之间通信
class MessageQuque {//消息队列集合
    private LinkedListlist = new LinkedList<>();
    //队列容量
    private int capcity;
    public MessageQuque(int capcity) {this.capcity = capcity;
    }
    //获取消息
    public Message take() {//检查队列是否为空
        synchronized (list) {while (list.isEmpty()) {try {list.wait();
                } catch (InterruptedException e) {e.printStackTrace();
                }
            }
            list.notifyAll();
            //从队列的头部获取消息并返回
            return list.removeFirst();
        }
    }
    //存入消息
    public void put(Message message) {synchronized (list) {//检查队列是否满了
            while (list.size() == capcity) {try {list.wait();
                } catch (InterruptedException e) {e.printStackTrace();
                }
            }
            list.addLast(message);
            list.notifyAll();
        }
    }
}
final class Message {private int id;
    private Object value;
    public int getId() {return id;
    }
    public Object getValue() {return value;
    }
    public Message(int id, Object value) {this.id = id;
        this.value = value;
    }
    @Override
    public String toString() {return "Message{" +
                "id=" + id +
                ", value=" + value +
                '}';
    }
} 你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧
