ScheduledExecutor Service 사용 노트
13446 단어 ScheduledExecutorServiceJava
그림으로 비교하면 이런 느낌이에요.
schedule() - 지정된 시간을 기다린 후 처리 import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class Main {
public static void main(String[] args) throws Exception {
ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
service.schedule(() -> {
System.out.println("end!!");
service.shutdown();
System.exit(0);
}, 1, TimeUnit.SECONDS);
int count = 0;
while (true) {
Thread.sleep(100);
System.out.println((++count) * 100 + " ms");
}
}
}
실행 결과100 ms
200 ms
300 ms
400 ms
500 ms
600 ms
700 ms
800 ms
900 ms
1000 ms
end!!
scheduleAtFixedRate() - 일정 시간마다 처리 import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
public class Main {
public static void main(String... args) throws Exception {
ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
AtomicInteger number = new AtomicInteger(0);
service.scheduleAtFixedRate(() -> {
int n = number.getAndIncrement();
System.out.println("begin(" + n + ")");
sleep(Math.max(1, 3-n));
System.out.println("end(" + n + ")");
}, 2, 2, TimeUnit.SECONDS);
int count = 0;
while (true) {
TimeUnit.SECONDS.sleep(1);
System.out.println((++count) + " s");
}
}
private static void sleep(long seconds) {
try {
TimeUnit.SECONDS.sleep(seconds);
} catch (InterruptedException e) {}
}
}
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class Main {
public static void main(String[] args) throws Exception {
ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
service.schedule(() -> {
System.out.println("end!!");
service.shutdown();
System.exit(0);
}, 1, TimeUnit.SECONDS);
int count = 0;
while (true) {
Thread.sleep(100);
System.out.println((++count) * 100 + " ms");
}
}
}
실행 결과100 ms
200 ms
300 ms
400 ms
500 ms
600 ms
700 ms
800 ms
900 ms
1000 ms
end!!
scheduleAtFixedRate() - 일정 시간마다 처리 import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
public class Main {
public static void main(String... args) throws Exception {
ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
AtomicInteger number = new AtomicInteger(0);
service.scheduleAtFixedRate(() -> {
int n = number.getAndIncrement();
System.out.println("begin(" + n + ")");
sleep(Math.max(1, 3-n));
System.out.println("end(" + n + ")");
}, 2, 2, TimeUnit.SECONDS);
int count = 0;
while (true) {
TimeUnit.SECONDS.sleep(1);
System.out.println((++count) + " s");
}
}
private static void sleep(long seconds) {
try {
TimeUnit.SECONDS.sleep(seconds);
} catch (InterruptedException e) {}
}
}
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
public class Main {
public static void main(String... args) throws Exception {
ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
AtomicInteger number = new AtomicInteger(0);
service.scheduleAtFixedRate(() -> {
int n = number.getAndIncrement();
System.out.println("begin(" + n + ")");
sleep(Math.max(1, 3-n));
System.out.println("end(" + n + ")");
}, 2, 2, TimeUnit.SECONDS);
int count = 0;
while (true) {
TimeUnit.SECONDS.sleep(1);
System.out.println((++count) + " s");
}
}
private static void sleep(long seconds) {
try {
TimeUnit.SECONDS.sleep(seconds);
} catch (InterruptedException e) {}
}
}
1 s
begin(0)
2 s
3 s
4 s
end(0)
begin(1) ←直前の処理が終了するまで待機させられている
5 s
6 s
end(1)
begin(2)
7 s
end(2)
begin(3)
8 s
end(3)
9 s
begin(4)
10 s
end(4)
11 s
begin(5)
12 s
end(5)
13 s
begin(6)
14 s
end(6)
15 s
첫 번째 파라미터의 처리가 완료되지 않아도 대기 시간이 지나면 gan-gan 처리라고 부른다.(2020년 3월 11일, 논평 지적 및 수정)
scheduleWithFixedDelay() - 중복 처리가 완료될 때까지 일정 시간 기다림 import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class Main {
private static int n = 0;
public static void main(String[] args) throws Exception {
ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
service.scheduleWithFixedDelay(() -> {
System.out.println("begin(" + n + ")");
sleep(1000);
System.out.println("end(" + n + ")");
n++;
}, 2, 1, TimeUnit.SECONDS);
int count = 0;
while (true) {
sleep(1000);
System.out.println((++count) + " s");
}
}
private static void sleep(long ms) {
try {
Thread.sleep(ms);
} catch (InterruptedException e) {}
}
}
실행 결과1 s
2 s
begin(0)
3 s
end(0)
4 s
begin(1)
5 s
end(1)
6 s
begin(2)
7 s
end(2)
8 s
begin(3)
9 s
end(3)
10 s
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class Main {
private static int n = 0;
public static void main(String[] args) throws Exception {
ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
service.scheduleWithFixedDelay(() -> {
System.out.println("begin(" + n + ")");
sleep(1000);
System.out.println("end(" + n + ")");
n++;
}, 2, 1, TimeUnit.SECONDS);
int count = 0;
while (true) {
sleep(1000);
System.out.println((++count) + " s");
}
}
private static void sleep(long ms) {
try {
Thread.sleep(ms);
} catch (InterruptedException e) {}
}
}
1 s
2 s
begin(0)
3 s
end(0)
4 s
begin(1)
5 s
end(1)
6 s
begin(2)
7 s
end(2)
8 s
begin(3)
9 s
end(3)
10 s
참고 자료
Reference
이 문제에 관하여(ScheduledExecutor Service 사용 노트), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/opengl-8080/items/ee8e926cf75e4d6058a2텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)