본 글에서는 JAVA 흐름제어와 슈퍼흐름제어 이후의 지연처리 방법에 대해 설명한다. 참고할 수 있도록 모든 사람과 공유하세요. 구체적인 구현 방법은 다음과 같습니다.
흐름 제어 확인(0.5초마다 누적되므로 최소 공백 임계값은 초당 2개 항목만 가능):
다음과 같이 코드를 복사합니다. import java.text.SimpleDateFormat;
java.util.Date 가져오기;
import java.lang.Thread;
/**
* 흐름 제어
*
* @author 첸스
*/
공개 클래스 OverflowController {
private int maxSendCountPerSecend; // 이 링크의 흐름 제어 임계값
개인 날짜 sendTime = 새 날짜();
private int sendCount = 0; //이 링크로 전송된 숫자
공개 OverflowController(int maxSendCountPerSecend) {
if (maxSendCountPerSecend < 2) {
maxSendCountPerSecend = 2;
}
this.maxSendCountPerSecend = maxSendCountPerSecend;
}
공개 int getMaxSendCountPerSecend() {
if (getMilliseconds(new Date()) >= 500) {
maxSendCountPerSecend / 2를 반환합니다.
}
return maxSendCountPerSecend - (maxSendCountPerSecend / 2);
}
/**
* 슈퍼플로우 제어인지 여부
*/
공개 부울 isOverflow(int sendNum) {
동기화됨 (이것) {
현재 날짜 = new Date();
if (now.getTime() - sendTime.getTime() >= 500) {
sendTime = 지금;
sendCount = sendNum;
} 또 다른 {
if (sendCount + sendNum > getMaxSendCountPerSecend()) {
사실을 반환;
} 또 다른 {
sendCount += sendNum;
}
}
거짓을 반환;
}
}
/**
* 지정된 시간의 밀리초 수를 가져옵니다.
*/
private int getMilliseconds(날짜 날짜) {
SimpleDateFormat df = new SimpleDateFormat("SSS");
return Integer.valueOf(df.format(date));
}
public static void main(String[] args)가 InterruptedException을 발생시킵니다.
OverflowController oc = 새로운 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()));
} 또 다른 {
System.out.println(i + "-sendOk-" + df.format(new Date()));
}
Thread.sleep(10);
}
}
}
Java에는 .net의 "지연 위임"이 없기 때문에 슈퍼 흐름 제어 후 처리가 지연됩니다.
다음과 같이 코드를 복사합니다. ThreadPool.RegisterWaitForSingleObject(
WaitHandle waitObject,
WaitOrTimerCallback 콜백,
객체 상태,
정수 밀리초TimeOutInterval,
bool ExecutionOnlyOnce
)
간단한 지연 대기열을 Java로 구현해야 합니다.
다음과 같이 코드를 복사합니다. import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;
공개 클래스 DelayEntry는 지연된 {를 구현합니다.
개인 정수 개수;
private long dequeuedTimeMillis; 대기열에서 빼는 시간
공개 int getCount() {
반환 횟수;
}
공개 무효 setCount(int 개수) {
this.count = 개수;
}
공개 긴 getDequeuedTimeMillis() {
dequeuedTimeMillis를 반환합니다.
}
공개 DelayEntry(긴 지연Millis) {
dequeuedTimeMillis = System.currentTimeMillis() + DelayMillis;
}
@보수
공개 int 비교To(지연 o) {
DelayEntry de = (DelayEntry) o;
긴 시간 초과 = dequeuedTimeMillis - de.dequeuedTimeMillis;
반환 시간 초과 > 0 ? 1: 시간 초과 < 0 ? -1: 0;
}
@보수
공개 긴 getDelay(TimeUnit 단위) {
dequeuedTimeMillis 반환 - System.currentTimeMillis();
}
}
다음과 같이 코드를 복사합니다. import java.util.concurrent.DelayQueue;
공개 클래스 DelayService {
공개 무효 실행() {
DelayQueue<DelayEntry> 대기열 = 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());
대기열.추가(de);
}
}
DelayConsumer 클래스는 Thread {를 확장합니다.
DelayQueue<DelayEntry> 큐;
공개 DelayConsumer(DelayQueue<DelayEntry> 대기열) {
this.queue = 대기열;
}
공개 무효 실행() {
동안 (참) {
노력하다 {
DelayEntry de = queue.take();
System.out.println("queue size=" + queue.size());
System.out.println(de.getCount());
System.out.println(System.currentTimeMillis());
} 잡기(InterruptedException e) {
e.printStackTrace();
}
}
}
}
공개 정적 무효 메인(String[] args) {
DelayService ds = new DelayService();
ds.run();
}
}
이 글이 모든 사람의 Java 프로그래밍에 도움이 되기를 바랍니다.