[JAVA] Latency processing after flow control and hyper-flow control

Source: Internet
Author: User
Tags date now

Flow control Check (cumulative per half-second, so the minimum blank threshold is only 2 per second):

Import Java.text.simpledateformat;import java.util.date;import java.lang.thread;/** * Flow control * * @author Chenx */public CLA SS Overflowcontroller {private int maxsendcountpersecend;//The link flow control threshold private Date sendtime = new Date ();p rivate int SENDC Ount = 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 ()) >=) {return MAXSENDCOUNTPERSECEND/2;} Return Maxsendcountpersecend-(MAXSENDCOUNTPERSECEND/2);} /** * Whether the hyper-flow control */public boolean isoverflow (int sendnum) {synchronized (this) {date now = new Date (); if (Now.gettime ()-Sendt Ime.gettime () >= () {sendtime = Now;sendcount = Sendnum;} else {if (Sendcount + sendnum > Getmaxsendcountpersecend ()) {return true;} else {sendcount + = Sendnum;}} return false;}} /** * Gets 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 <=; 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);}}

The deferred processing after the hyper-flow control, because there is no. NET "deferred delegation" in Java, says:

ThreadPool.RegisterWaitForSingleObject (
WaitHandle Waitobject,
WaitOrTimerCallback CallBack,
Object State,
int millisecondsTimeOutInterval,
BOOL executeOnlyOnce

)

Java requires a simple delay queue to be implemented:

Import Java.util.concurrent.delayed;import Java.util.concurrent.timeunit;public class Delayentry implements Delayed { private int count;private long dequeuedtimemillis; Out-of-queue time 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.dequeuedti Memillis;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> ();D elayconsumer delayconsumer = new Delayconsumer (queue);d Elayconsumer.start (); for (int i = 0; i < i++) {delayentry de = new Delayentry (;d) e.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 ();d s.run ();}}

[JAVA] streaming and post-flow-controlled latency processing

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.