JAVA examples of delayed processing after flow control and hyperflow control

  • 2020-04-01 03:37:50
  • OfStack

In this paper, the example of JAVA flow control and after the flow control of the delay processing method. Share with you for your reference. The specific implementation method is as follows:

Flow control check (accumulates every half second, so the minimum blank threshold can only be 2 pieces per second) :

import java.text.SimpleDateFormat;
import java.util.Date;
import java.lang.Thread;
 
/**
 * Flow control
 *
 * @author chenx
 */
public class OverflowController {
 
    private int maxSendCountPerSecend; //The link upstream control threshold
    private Date sendTime = new Date();
    private int sendCount = 0; //The 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);
    }
 
    /**
     * Whether or not overflow 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 at 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);
        }
    }
}

Delay processing after hyperflow control, because there is no "delay delegate" in.net in Java:
ThreadPool.RegisterWaitForSingleObject(
 WaitHandle waitObject,
      WaitOrTimerCallback callBack,
      Object state,
     int millisecondsTimeOutInterval,
     bool executeOnlyOnce
)

Java needs to implement a simple delay queue:

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;
 
public class DelayEntry implements Delayed {
 
    private int count;
    private long dequeuedTimeMillis; //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();
    }
}

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

I hope this article has been helpful to your Java programming.


Related articles: