Dieser Artikel beschreibt die Verzögerungsverarbeitungsmethode nach JAVA-Flusskontrolle und Super-Flusskontrolle. Teilen Sie es als Referenz mit allen. Die spezifische Implementierungsmethode ist wie folgt:
Flusskontrollprüfung (wird jede halbe Sekunde akkumuliert, daher kann der minimale Leerschwellenwert nur 2 Elemente pro Sekunde betragen):
Kopieren Sie den Code wie folgt: import java.text.SimpleDateFormat;
import java.util.Date;
java.lang.Thread importieren;
/**
* Flusskontrolle
*
* @author chenx
*/
öffentliche Klasse OverflowController {
private int maxSendCountPerSecend; // Flusskontrollschwelle für diesen Link
privates Datum sendTime = new Date();
private int sendCount = 0; //Die über diesen Link gesendete Nummer
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);
}
/**
* Ob es sich um eine Super-Flow-Kontrolle handelt
*/
öffentlicher boolescher Wert isOverflow(int sendNum) {
synchronisiert (dies) {
Datum jetzt = neues Datum();
if (now.getTime() - sendTime.getTime() >= 500) {
sendTime = now;
sendCount = sendNum;
} anders {
if (sendCount + sendNum > getMaxSendCountPerSecend()) {
return true;
} anders {
sendCount += sendNum;
}
}
return false;
}
}
/**
* Ermitteln Sie die Anzahl der Millisekunden in der angegebenen Zeit
*/
private int getMilliseconds(Date date) {
SimpleDateFormat df = new SimpleDateFormat("SSS");
return Integer.valueOf(df.format(date));
}
public static void main(String[] args) löst 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()));
} anders {
System.out.println(i + "-sendOk-" + df.format(new Date()));
}
Thread.sleep(10);
}
}
}
Verzögern Sie die Verarbeitung nach der Super-Flow-Kontrolle, da es in Java keine „Verzögerungsdelegierung“ von .net gibt:
Kopieren Sie den Code wie folgt: ThreadPool.RegisterWaitForSingleObject(
WaitHandle waitObject,
WaitOrTimerCallback callBack,
Objektstatus,
int MillisekundenTimeOutInterval,
boolexecuteOnlyOnce
)
Eine einfache Verzögerungswarteschlange muss in Java implementiert werden:
Kopieren Sie den Code wie folgt: import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;
Die öffentliche Klasse DelayEntry implementiert Delayed {
private int count;
private long dequeuedTimeMillis; // Zeit aus der Warteschlange
public int getCount() {
Rückgabeanzahl;
}
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;
Rückgabezeitüberschreitung > 0 ? 1 : Zeitüberschreitung < 0 ? -1 : 0;
}
@Override
public long getDelay(TimeUnit-Einheit) {
return dequeuedTimeMillis - System.currentTimeMillis();
}
}
Kopieren Sie den Code wie folgt: import java.util.concurrent.DelayQueue;
öffentliche Klasse 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);
}
}
Klasse DelayConsumer erweitert Thread {
DelayQueue<DelayEntry>-Warteschlange;
public DelayConsumer(DelayQueue<DelayEntry> queue) {
this.queue = Warteschlange;
}
public void run() {
while (wahr) {
versuchen {
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();
}
}
Ich hoffe, dass dieser Artikel für die Java-Programmierung aller hilfreich sein wird.