[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它是流为基础进行输入输出的.全部数据被串行化写入输出流,或者从输入流读入. -- 百度百科 ...
随机推荐
- ubuntu创建wifi热点plasma-nm
第一步:安装 plasma-nm sudo apt-get install plasma-nm 第二步:启动程序 可以使用 Alt+F2 后,搜索 kde-nm-connection-editor 也 ...
- How do I avoid capturing self in blocks when implementing an API?
Short answer Instead of accessing self directly, you should access it indirectly, from a reference t ...
- java程序员修炼之前笔记(前半部分)
第一部分 用java7做开发 第一章 初始java7 java7中的新特性 switch支持String 支持100_000_000数值表示法 新的异常处理 | 连接多个异常 final Except ...
- python文件操作os模块
Python 统计某一文件夹下文件数量 使用python pathlib模块 from pathlib import Path dir_path = ' ' print(len(list(Path( ...
- java外观模式
外观模式是为了解决类与类之家的依赖关系的,像spring一样,可以将类和类之间的关系配置到配置文件中,而外观模式就是将他们的关系放在一个Facade类中,降低了类类之间的耦合度,该模式中没有涉及到接口 ...
- How to:Aborting a long running task in TPL
http://social.msdn.microsoft.com/Forums/vstudio/en-US/d0bcb415-fb1e-42e4-90f8-c43a088537fb/aborting- ...
- 数数(高维DP)
T1 数数 [问题描述] fadbec 很善于数数,⽐如他会数将 a 个红球,b 个黄球,c 个蓝球,d 个绿球排成⼀列,任意相邻不同⾊的数⽬. 现在 R 君不知道 fadbec 数的对不对,想让你也 ...
- Subway Pursuit (二分)(交互题)
题目来源:codeforces1039B Subway Pursuit 题目大意: 在1到n里有一个运动的点,要求找到这个点,每次可以查询一个区间内有没有这个点,每次这个点往左或者往右移动1到k个位置 ...
- Jenkins 主题分享
写在前面的话 Jenkins 现在是我使用的最多的 CI/CD 工具,但是一直无法接受他的前端,于是闲暇之余查了一些关于 Jenkins 更换样式的方法,于是自己给他写了个套子,感兴趣的老哥以下是项目 ...
- 17、xtrabackup 常用备份功能与选项
并行备份 > innobackupex -p123123 --parallel=8 /backup 节流备份 > innobackupex -p123123 --throttle=200 ...