[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;
} @Override
public int compareTo(Delayed o) {
DelayEntry de = (DelayEntry) o;
long timeout = dequeuedTimeMillis - de.dequeuedTimeMillis;
return timeout > 0 ? 1 : timeout < 0 ? -1 : 0;
} @Override
public 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]流控及超流控后的延迟处理的更多相关文章
- Sentinel源码解析四(流控策略和流控效果)
引言 在分析Sentinel的上一篇文章中,我们知道了它是基于滑动窗口做的流量统计,那么在当我们能够根据流量统计算法拿到流量的实时数据后,下一步要做的事情自然就是基于这些数据做流控.在介绍Sentin ...
- Java socket中关闭IO流后,发生什么事?(以关闭输出流为例)
声明:该博文以socket中,关闭输出流为例进行说明. 为了方便讲解,我们把DataOutputstream dout = new DataOutputStream(new BufferedOutpu ...
- Java基础系列8——IO流超详细总结
该系列博文会告诉你如何从入门到进阶,一步步地学习Java基础知识,并上手进行实战,接着了解每个Java知识点背后的实现原理,更完整地了解整个Java技术体系,形成自己的知识框架. 在初学Java时,I ...
- 无比迅速敏捷地开发iOS超精美控件
目录 前言 设计 编码 PaintCode 前言 自从人生第一篇博客<iOS中的预编译指令的初步探究>问世以来 浏览量竟然达到了360多,(路过的大神勿笑!)这些浏览量使我兴奋异常但又令我 ...
- 十八、Java基础--------IO流体系以及字符流
在上一章节中详细介绍集合框架的相关知识,在接下来的几篇文章中将讲述Java中另一个及其重要的知识——IO流,本文主要是讲述IO流的一些基本概念以及字符流的相关应用. IO流 介绍IO流之前先介绍一下什 ...
- Java:IO流其他类(字节数组流、字符数组流、数据流、打印流、Properities、对象流、管道流、随机访问、序列流、字符串读写流)
一.字节数组流: 类 ByteArrayInputStream:在构造函数的时候,需要接受数据源,而且数据源是一个字节数组. 包含一个内部缓冲区,该缓冲区包含从流中读取的字节.内部计数器跟踪 read ...
- Java IO 技术之基本流类
流式IO 流(Stream)是字节的源或目的. 两种基本的流是:输入流(Input Stream)和输出流(Output Stream).可从中读出一系列字节的对象称为输入流.而能向其 ...
- Java——(九)IO流
一.流的分类 1.输入流和输出流 按照流的流向来分,可以分为输入流和输出流 输入流:只能从中读取数据,而不能向其写入数据. 输出流:只能向其写入数据,而不能从中读取数据. 此处的输入.输出涉及一个方向 ...
- Java I/O— 梳理各种“流”
背景 Java核心库java.io它提供了一个综合IO接口.包含:文件读写.标准装备输出等..Java在IO它是流为基础进行输入输出的.全部数据被串行化写入输出流,或者从输入流读入. -- 百度百科 ...
随机推荐
- (转)UI设计实战篇——利用Bootstrap框架制作查询页面的界面
原文地址:http://www.cnblogs.com/grenet/p/3413085.html Bootstrap框架是一个前端UI设计的框架,它提供了统一的UI界面,简化了设计界面UI的过程(缺 ...
- Cannot evaluate the property expression "$([MSBuild]::ValueOrDefault('$(VCTargetsPath)','$(MSBuildExtensionsPath32)\Microsoft.Cpp\v4.0\V140\'))" found at "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSBuil
Cannot evaluate the property expression "$([MSBuild]::ValueOrDefault('$(VCTargetsPath)','$(MSBu ...
- Find out your Java heap memory size
In this article, we will show you how to use the -XX:+PrintFlagsFinal to find out your heap size det ...
- Reporting Service服务SharePoint集成模式安装配置(6、安装Reporting services Add-in for SharePoint 外接程序)
第五步骤 : 安装Reporting services Add-in for SharePoint 外接程序 RS 外接程序是在 SharePoint 服务器上运行用于 SharePoint 产品的 ...
- Tomcat负载均衡原理详解及配置(Apache2.2.19+Tomcat7.0.12)
结构图 JAVA项目一般直接用Tomcat作为Web服务器.为了增加tomcat的性能和稳定性,我们一般采用balance和session同步机制. 下图列出了我们常用也是最简单的解决方案. 说明 1 ...
- Service Worker 缓存文件处理
交代背景 前段时间升级了一波Google Chrome,发现我的JulyNovel站点Ctrl+F5也刷新不了,后来发现是新的Chrome已经支持Service Worker,而我的JulyNovel ...
- SQL函数(不定时更新)
sum求和:select sum(a) from table1 group by b 依据b列为a列求和 distinct去除重复:select distinct(a) from table1 ...
- Python 振动分析 迭代法计算高阶特征值及特征向量
参考书 : <<振动分析>> 张准 汪凤泉 编著 东南大学出版社 ISBN 7-80123-583-4 参考章节 : 4.6.2 和 4.6.3 <<数值分析> ...
- 21天学通C++学习笔记(八):指针和引用
1. 简述 C++最大的优点之一是,既可以用它来编写不依赖于机器(主要是内存)的高级应用程序,又可以用它来编写与硬件紧密协作的应用程序. 事实上C++让您能够在字节和比特级调整应用程序的性能,而要编写 ...
- C# 调用C++动态库注意事项
C# 调用C++动态库注意事项 最近项目上需要在C#中调用C++,期间遇到不少坑,总结如下: 1.in const char* 对应C#中string 或 IntPtr 2.out const ...