Este artigo descreve o método de processamento de atraso após o controle de fluxo JAVA e o controle de superfluxo. Compartilhe com todos para sua referência. O método de implementação específico é o seguinte:
Verificação de controle de fluxo (acumulada a cada meio segundo, portanto o limite mínimo em branco pode ser de apenas 2 itens por segundo):
Copie o código da seguinte forma: import java.text.SimpleDateFormat;
importar java.util.Date;
importar java.lang.Thread;
/**
* Controle de fluxo
*
* @autor Chenx
*/
classe pública OverflowController {
private int maxSendCountPerSecend; // Limite de controle de fluxo neste link
data privada sendTime = new Date();
private int sendCount = 0; //O número enviado neste link
public OverflowController(int maxSendCountPerSecend) {
if (maxSendCountPerSecend <2) {
maxSendCountPerSecend = 2;
}
this.maxSendCountPerSecend = maxSendCountPerSecend;
}
public int getMaxSendCountPerSecend() {
if (getMiliseconds(new Date()) >= 500) {
retornar maxSendCountPerSecend/2;
}
retornar maxSendCountPerSecend - (maxSendCountPerSecend/2);
}
/**
* Quer se trate de super controle de fluxo
*/
public boolean isOverflow(int sendNum) {
sincronizado (este) {
Data agora = new Date();
if (now.getTime() - sendTime.getTime() >= 500) {
enviarHora = agora;
enviarContagem = enviarNum;
} outro {
if (sendCount + sendNum > getMaxSendCountPerSecend()) {
retornar verdadeiro;
} outro {
enviarCount += enviarNum;
}
}
retornar falso;
}
}
/**
* Obtenha o número de milissegundos no tempo especificado
*/
private int getMilliseconds(Data data) {
SimpleDateFormat df = new SimpleDateFormat("SSS");
return Integer.valueOf(df.format(data));
}
public static void main(String[] args) lança InterruptedException {
OverflowController oc = novo OverflowController(50);
SimpleDateFormat df = new SimpleDateFormat("aaaa-MM-dd HH:mm:ss:SSS");
for (int i = 0; i <= 100; i++) {
if (oc.isOverflow(1)) {
System.out.println(i + "-isOverflow-" + df.format(nova data()));
} outro {
System.out.println(i + "-sendOk-" + df.format(nova data()));
}
Thread.sleep(10);
}
}
}
Atraso no processamento após super controle de fluxo, porque não há "delegação de atraso" de .net em Java:
Copie o código da seguinte forma: ThreadPool.RegisterWaitForSingleObject(
WaitHandle waitObject,
WaitOrTimerCallback callBack,
Estado do objeto,
int milissegundosTimeOutInterval,
bool executeOnlyOnce
)
Uma fila de atraso simples precisa ser implementada em Java:
Copie o código da seguinte maneira: import java.util.concurrent.Delayed;
importar java.util.concurrent.TimeUnit;
classe pública DelayEntry implementa Delayed {
contagem interna privada;
private long dequeuedTimeMillis; // tempo de desenfileiramento
public int getContagem() {
contagem de retorno;
}
public void setCount(int contagem) {
isto.contagem = contagem;
}
público longo getDequeuedTimeMillis() {
return dequeuedTimeMillis;
}
public DelayEntry(long delayMillis) {
dequeuedTimeMillis = System.currentTimeMillis() + delayMillis;
}
@Substituir
public int compareTo(Atrasado) {
DelayEntry de = (DelayEntry) o;
tempo limite longo = dequeuedTimeMillis - de.dequeuedTimeMillis;
tempo limite de retorno > 0 ? 1 : tempo limite < 0 ?
}
@Substituir
público longo getDelay(Unidade TimeUnit) {
return dequeuedTimeMillis - System.currentTimeMillis();
}
}
Copie o código da seguinte maneira: import java.util.concurrent.DelayQueue;
classe pública DelayService {
execução void pública() {
DelayQueue<DelayEntry> fila = new DelayQueue<DelayEntry>();
DelayConsumer delayConsumer = new DelayConsumer(fila);
delayConsumer.start();
for (int i = 0; i < 100; i++) {
DelayEntry de = novo DelayEntry(5000);
de.setCount(i);
System.out.println(System.currentTimeMillis() + "--------" + de.getCount());
fila.add(de);
}
}
classe DelayConsumer estende Thread {
fila DelayQueue<DelayEntry>;
public DelayConsumer(DelayQueue<DelayEntry> fila) {
this.queue = fila;
}
execução void pública() {
enquanto (verdadeiro) {
tentar {
DelayEntry de = queue.take();
System.out.println("tamanho da fila=" + 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();
}
}
Espero que este artigo seja útil para a programação Java de todos.