From b3b17b231e2f2840332ce6eb96f791865fdec6d5 Mon Sep 17 00:00:00 2001
From: zhubaomin <zhubaomin>
Date: 星期一, 14 四月 2025 16:38:16 +0800
Subject: [PATCH] 代码优化
---
pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/queue/Queue.java | 186 ++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 186 insertions(+), 0 deletions(-)
diff --git a/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/queue/Queue.java b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/queue/Queue.java
new file mode 100644
index 0000000..cfa77d5
--- /dev/null
+++ b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/queue/Queue.java
@@ -0,0 +1,186 @@
+package com.dy.common.queue;
+
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+/**
+ * 闃熷垪锛屽厛杩涘厛鍑�
+ */
+public class Queue {
+ @SuppressWarnings("unfinal")
+ private final Logger log = LogManager.getLogger(Queue.class.getName()) ;
+
+ private final Object synObj ;
+
+ private String queueName ;
+
+ private final Node head ;
+ private final Node tail ;
+ private int size ;
+
+ //鏈�澶ч檺鍒讹紝瓒呰繃杩欎釜闄愬埗锛岄槦鍒楀皢鎷掔粷push璋冪敤锛屽苟鎶涘嚭寮傚父
+ private int maxSize ;
+ //璀﹀憡闄愬埗锛岃秴杩囪繖涓檺鍒讹紝闃熷垪鎺ユ敹push鍚庯紝灏嗚褰晈arn鏃ュ織
+ private int warnSize ;//
+
+
+ public Queue(String queueName){
+ synObj = new Object() ;
+ this.queueName = queueName ;
+ if(this.queueName == null){
+ this.queueName = Queue.class.getName() ;
+ }
+ this.head = new Node() ;
+ this.tail = new Node() ;
+ this.head.next = this.tail ;
+ this.tail.pre = this.head ;
+ this.size = 0 ;
+
+ this.warnSize = 1000000 ;
+ this.maxSize = Integer.MAX_VALUE ;
+ }
+
+ public void setLimit(int warnSize, int maxSize){
+ this.warnSize = warnSize ;
+ this.maxSize = maxSize ;
+ if(this.warnSize <= 0){
+ this.warnSize = 1 ;
+ }
+ if(this.warnSize == Integer.MAX_VALUE){
+ this.warnSize = Integer.MAX_VALUE - 1000 ;
+ }
+
+ if(this.maxSize <= 0){
+ this.maxSize = 1 ;
+ }
+ }
+ /**
+ * 姝ゆ柟娉曞湪澶氱嚎绋嬩腑鎵ц锛屽悓姝ユ柟寮忥紝闃叉鍏朵粬绾跨▼鎿嶄綔鍏朵粬鏂规硶鏃朵笉鍚屾
+ * 鍦ㄩ槦鍒楀熬閮ㄥ帇鍏ヤ竴鑺傜偣
+ * 鍦ㄥ绾跨▼涓簲鐢紝鍔犲悓姝�
+ * @param obj 鍏ュ垪鐨勫璞�
+ */
+ @SuppressWarnings("unused")
+ public void pushHead(NodeObj obj)throws Exception{
+ synchronized(synObj){
+ if(obj == null){
+ return ;
+ }
+ if(this.size >= this.maxSize){
+ throw new Exception("闃熷垪锛�" + queueName + "锛夌紦瀛樿妭鐐规暟" + this.size + " 瓒呭嚭鏈�澶ч檺鍒�(" + this.maxSize + ")锛屾嫆缁濇帴鏀舵柊鏁版嵁銆�");
+ }
+ Node node = new Node();
+ node.obj = obj ;
+ node.next = this.head.next ;
+ node.pre = this.head ;
+ this.head.next.pre = node ;
+ this.head.next = node ;
+ this.size ++ ;
+ if(this.size >= this.warnSize){
+ log.warn("闃熷垪锛�" + queueName + "锛夌紦瀛樿妭鐐规暟 " + this.size + " 瓒呴檺(" + this.warnSize + ")璀﹀憡銆�");
+ }
+ }
+ }
+
+ /**
+ * 姝ゆ柟娉曞湪澶氱嚎绋嬩腑鎵ц锛屽悓姝ユ柟寮忥紝闃叉鍏朵粬绾跨▼鎿嶄綔鍏朵粬鏂规硶鏃朵笉鍚屾
+ * 鍦ㄩ槦鍒楀熬閮ㄥ帇鍏ヤ竴鑺傜偣
+ * 鍦ㄥ绾跨▼涓簲鐢紝鍔犲悓姝�
+ * @param obj 鍏ュ垪鐨勫璞�
+ */
+ public void pushTail(NodeObj obj)throws Exception{
+ synchronized(synObj){
+ if(obj == null){
+ return ;
+ }
+ if(this.size >= this.maxSize){
+ throw new Exception("闃熷垪锛�" + queueName + "锛夌紦瀛樿妭鐐规暟" + this.size + " 瓒呭嚭鏈�澶ч檺鍒�(" + this.maxSize + ")锛屾嫆缁濇帴鏀舵柊鏁版嵁銆�");
+ }
+ Node node = new Node();
+ node.obj = obj ;
+ node.next = this.tail ;
+ node.pre = this.tail.pre ;
+ this.tail.pre.next = node ;
+ this.tail.pre = node ;
+ this.size ++ ;
+ if(this.size >= this.warnSize){
+ log.warn("闃熷垪锛�" + queueName + "锛夌紦瀛樿妭鐐规暟 " + this.size + " 瓒呴檺(" + this.warnSize + ")璀﹀憡銆�");
+ }
+ }
+ }
+
+ /**
+ * 姝ゆ柟娉曞湪鍗曠嚎绋嬩腑鎵ц锛屼絾鍙兘鍙楄皟鐢╬ush鐨勫绾跨▼褰卞搷锛屾墍浠ヤ篃鐢ㄥ埌浜嗗悓姝�
+ * 寮瑰嚭绗竴涓妭鐐�
+ * 鍦ㄥ绾跨▼涓簲鐢紝鍔犲悓姝�
+ * @return 鍑哄垪瀵硅薄
+ */
+ public NodeObj pop(){
+ synchronized(synObj){
+ NodeObj obj = null ;
+ if(this.size > 0){
+ Node node = this.head.next ;
+ if(node != this.tail){
+ obj = node.obj ;
+ this.head.next = this.head.next.next ;
+ this.head.next.pre = this.head ;
+ node.next = null ;
+ node.pre = null ;
+ this.size -- ;
+ }
+ }
+ return obj ;
+ }
+ }
+
+ /**
+ * 寰楀埌绗竴涓妭鐐癸紝浣嗕笉鎶婅妭鐐逛粠闃熷垪涓竻闄�
+ * @return 绗竴涓璞�
+ */
+ @SuppressWarnings("unused")
+ public Node getFirstNode(){
+ Node node = this.head.next ;
+ if(node != this.tail){
+ return node ;
+ }
+ return null ;
+ }
+ /**
+ * 寰楀埌鏈�鍚庝竴涓妭鐐癸紝浣嗕笉鎶婅妭鐐逛粠闃熷垪涓竻闄�
+ * @return 鏈�鍚庝竴涓璞�
+ */
+ @SuppressWarnings("unused")
+ public Node getLastNode(){
+ Node node = this.tail.pre ;
+ if(node != this.head){
+ return node ;
+ }
+ return null ;
+ }
+
+ /**
+ * 娓呴櫎涓�涓妭鐐�
+ * @param node 鍒犻櫎涓�涓妭鐐�
+ */
+ @SuppressWarnings("unused")
+ public void remove(Node node){
+ synchronized(synObj){
+ if(node != null && node != this.head && node != this.tail){
+ node.pre.next = node.next ;
+ node.next.pre = node.pre ;
+ node.next = null ;
+ node.pre = null ;
+ this.size -- ;
+ }
+ }
+ }
+
+ /**
+ * 寰楀埌闃熷垪涓湁鏁堣妭鐐逛釜鏁�
+ * @return 闃熷垪鍐呰妭鐐规暟閲�
+ */
+ public int size(){
+ return this.size ;
+ }
+
+}
\ No newline at end of file
--
Gitblit v1.8.0