[JAVA] latency processing after throttling and superthrottling, java
Traffic control check (accumulated every half second, so the minimum left blank threshold can only be 2 per second ):
Import java. text. simpleDateFormat; import java. util. date; import java. lang. thread;/*** traffic control *** @ author chenx */public class OverflowController {private int maxSendCountPerSecend; // The threshold value of this link is private Date sendTime = new Date (); private int sendCount = 0; // The number of messages sent on this link is 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);}/*** whether to throttling */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;}/*** get the number of milliseconds for the specified time */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 );}}}
Due to the absence of. net "delay delegation" in java, the latency processing after the superflow control is implemented:
ThreadPool. RegisterWaitForSingleObject (
WaitHandle waitObject,
WaitOrTimerCallback callBack,
Object state,
Int millisecondsTimeOutInterval,
Bool executeOnlyOnce
)
Java requires a simple latency queue:
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. curren TTimeMillis () + delayMillis;} @ Overridepublic int compareTo (Delayed o) {DelayEntry de = (DelayEntry) o; long timeout = queue-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();}}