国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

[JAVA]流控及超流控后的延遲處理

2019-11-14 22:25:20
字體:
來源:轉載
供稿:網友
[java]流控及超流控后的延遲處理

流控檢查(每半秒累計,因此最小留空閥值只能做到每秒2條):

import java.text.SimpleDateFormat;import java.util.Date;import java.lang.Thread;/** * 流量控制 *  * @author chenx */public class OverflowController {PRivate int maxSendCountPerSecend; // 該條鏈路上流控閥值private Date sendTime = new Date();private int sendCount = 0; // 該條鏈路上發送的數量public OverflowController(int maxSendCountPerSecend) {if (maxSendCountPerSecend < 2) {maxSendCountPerSecend = 2;}this.maxSendCountPerSecend = maxSendCountPerSecend;}public int getMaxSendCountPerSecend() {if (getMilliseconds(new Date()) >= 500) {return maxSendCountPerSecend / 2;}return maxSendCountPerSecend - (maxSendCountPerSecend / 2);}/** * 是否超流控 */public boolean isOverflow(int sendNum) {synchronized (this) {Date now = new Date();if (now.getTime() - sendTime.getTime() >= 500) {sendTime = now;sendCount = sendNum;} else {if (sendCount + sendNum > getMaxSendCountPerSecend()) {return true;} else {sendCount += sendNum;}}return false;}}/** * 獲取指定時間的毫秒數 */private int getMilliseconds(Date date) {SimpleDateFormat df = new SimpleDateFormat("SSS");return Integer.valueOf(df.format(date));}public static void main(String[] args) throws InterruptedException {OverflowController oc = new OverflowController(50);SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");for (int i = 0; i <= 100; i++) {if (oc.isOverflow(1)) {System.out.println(i + "-isOverflow-" + df.format(new Date()));} else {System.out.println(i + "-sendOk-" + df.format(new Date()));}Thread.sleep(10);}}}

超流控后的延遲處理,由于java中沒有.net的“延遲委托”一說:

ThreadPool.RegisterWaitForSingleObject(WaitHandle waitObject, WaitOrTimerCallback callBack, Object state, int millisecondsTimeOutInterval, bool executeOnlyOnce

)

Java下需實現一個簡單的延遲隊列:

import java.util.concurrent.Delayed;import java.util.concurrent.TimeUnit;public class DelayEntry implements Delayed {private int count;private long dequeuedTimeMillis; // 出隊列時間public int getCount() {return count;}public void setCount(int count) {this.count = count;}public long getDequeuedTimeMillis() {return dequeuedTimeMillis;}public DelayEntry(long delayMillis) {dequeuedTimeMillis = System.currentTimeMillis() + delayMillis;}@Overridepublic int compareTo(Delayed o) {DelayEntry de = (DelayEntry) o;long timeout = dequeuedTimeMillis - de.dequeuedTimeMillis;return timeout > 0 ? 1 : timeout < 0 ? -1 : 0;}@Overridepublic long getDelay(TimeUnit unit) {return dequeuedTimeMillis - System.currentTimeMillis();}}

import java.util.concurrent.DelayQueue;public class DelayService {public void run() {DelayQueue<DelayEntry> queue = new DelayQueue<DelayEntry>();DelayConsumer delayConsumer = new DelayConsumer(queue);delayConsumer.start();for (int i = 0; i < 100; i++) {DelayEntry de = new DelayEntry(5000);de.setCount(i);System.out.println(System.currentTimeMillis() + "--------" + de.getCount());queue.add(de);}}class DelayConsumer extends Thread {DelayQueue<DelayEntry> queue;public DelayConsumer(DelayQueue<DelayEntry> queue) {this.queue = queue;}public void run() {while (true) {try {DelayEntry de = queue.take();System.out.println("queue size=" + queue.size());System.out.println(de.getCount());System.out.println(System.currentTimeMillis());} catch (InterruptedException e) {e.printStackTrace();}}}}public static void main(String[] args) {DelayService ds = new DelayService();ds.run();}}


上一篇:java 讀取properties文件

下一篇:JDBC的總結

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 丰镇市| 隆子县| 泽州县| 休宁县| 分宜县| 平山县| 怀来县| 柳林县| 北流市| 扎赉特旗| 香格里拉县| 惠水县| 乌海市| 永吉县| 浦江县| 衡南县| 武冈市| 闻喜县| 庆阳市| 平度市| 盐池县| 梧州市| 林西县| 榆树市| 通山县| 察雅县| 临猗县| 孟村| 哈巴河县| 和林格尔县| 都兰县| 华亭县| 济源市| 栾川县| 新余市| 江油市| 忻州市| 阳谷县| 新郑市| 福清市| 梧州市|