A case of delay processing after Java flow control and hyper-flow control _java

Source: Internet
Author: User
Tags date now

This article describes the Java flow control and hyper-flow control after the delay processing method. Share to everyone for your reference. The implementation methods are as follows:

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

Copy Code code as follows:
Import Java.text.SimpleDateFormat;
Import Java.util.Date;
Import Java.lang.Thread;

/**
* Flow Control
*
* @author Chenx
*/
public class Overflowcontroller {

private int maxsendcountpersecend; The flow control valve value on the link road
Private date Sendtime = new Date ();
private int sendcount = 0; Number of messages sent on this link

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);
}

/**
* is the hyper-flow control
*/
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;
}
}

/**
* Gets the number of milliseconds for a 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);
}
}
}

After the hyper-streaming delay processing, because the Java does not have the. Net "deferred delegate" one says:
Copy Code code as follows:
ThreadPool.RegisterWaitForSingleObject (
WaitHandle Waitobject,
WaitOrTimerCallback CallBack,
Object State,
int millisecondsTimeOutInterval,
BOOL executeOnlyOnce
)

Java needs to implement a simple delay queue:

Copy Code code as follows:
Import java.util.concurrent.Delayed;
Import Java.util.concurrent.TimeUnit;

Public class Delayentry implements delayed {

private int count;
Private long Dequeuedtimemillis; Out 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;
}

@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 ();
}
}


Copy Code code as follows:
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 < 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 ();
}
}

I hope this article will help you with your Java programming.

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.