From 7f2edc4bf3b3d2ac429abb9a06ac7f425b2a01fd Mon Sep 17 00:00:00 2001 From: luxiaotao1123 <t1341870251@63.com> Date: 星期三, 28 十二月 2022 09:00:42 +0800 Subject: [PATCH] # --- src/main/java/com/zy/core/News.java | 27 +++++++++++++++++++++------ 1 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/zy/core/News.java b/src/main/java/com/zy/core/News.java index ce0133c..47b3c24 100644 --- a/src/main/java/com/zy/core/News.java +++ b/src/main/java/com/zy/core/News.java @@ -39,19 +39,25 @@ public NewsQueue(Class<T> cls, int capacity) { this.cls = cls; - arr = (T[]) Array.newInstance(cls, capacity); + this.arr = (T[]) Array.newInstance(cls, capacity); this.capacity = capacity; } public synchronized boolean offer(T t) { if (this.tail == this.capacity) { + this.peek(); + } + this.reform(); + this.arr[this.tail] = t; + this.tail ++; + return true; + } + + public synchronized boolean put(T t) { + if (this.tail == this.capacity) { return false; } else { - for (int i = this.head; i < this.tail; i++) { - this.arr[i-this.head] = this.arr[i]; - } - this.tail -= this.head; - this.head = 0; + this.reform(); } this.arr[this.tail] = t; this.tail ++; @@ -64,9 +70,18 @@ } T t = this.arr[this.head]; this.head ++; + this.reform(); return t; } + private void reform() { + for (int i = this.head; i < this.tail; i++) { + this.arr[i-this.head] = this.arr[i]; + } + this.tail -= this.head; + this.head = 0; + } + public synchronized int size() { return this.tail - this.head; } -- Gitblit v1.9.1