ν°μ€ν 리 λ·°
ExecutorService
π₯ μλ°μ μ€λ λλ₯Ό νΈνκ² κ΄λ¦¬νκΈ° μν λΌμ΄λΈλ¬λ¦¬λ‘ μ€λ λ νλ° μ€λ λ μμ± νΉμ κ·Έ μμ μ κ°λ₯νκ² νλ€.
newFixedThreadPool(int)
ποΈ ThreadPool (ThreadPoolExecutor) μμ±μ μν μ μ ν©ν°λ¦¬ λ©μλμ€ νλλ‘
μΈμ κ°μλ§νΌ κ³ μ λ ThreadPool μ λ§λ λ€.
// μ€λ λκ° 20κ°λ‘ κ³ μ λλ Thread Pool μμ±νλ€.
ExecutorService es = Executors.newFixedThreadPool(20);
newCachedThreadPool
ποΈ ThreadPool (ThreadPoolExecutor) μμ±μ μν μ μ ν©ν°λ¦¬ λ©μλμ€ νλλ‘ νμν λ, νμν λ§νΌ ThreadPool μ λ§λ λ€. μ΄λ―Έ μμ±λ μ°λ λλ₯Ό μ¬μ¬μ© ν΄ μ±λ₯μμ μ΄μ μ΄ μμ μ μλ€.
// Thread Pool μμ±
ExecutorService es = Executors.newCachedThreadPool();
newSingleThreadExecutor()
ποΈ μ°λ λ ν κ°μΈ ExecutorServiceλ₯Ό μμ±νλ μ μ ν©ν°λ¦¬ λ©μλλ€. μ±κΈ μ°λ λ μμ μ μ²λ¦¬ν λ μ¬μ©νλ€.
// μ±κΈ μ°λ λ ExecutorService λ₯Ό μμ±νλ€.
ExecutorService es = Executors.newSingleThreadExecutor();
execute(Runnable run)
π μλ‘μ΄ μ€λ λλ₯Ό ν΅ν΄ Runnable μ μ€ννλ€.
es.execute(() -> {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {}
log.info("Async");
});
log.info("Exit");
}
κ²°κ³Ό
07:49:58.256 [main] INFO asynchronous.toby.FutureEx - Exit
07:50:00.260 [pool-1-thread-1] INFO asynchronous.toby.FutureEx - Async
submit(Callable task)
π μλ‘μ΄ μ€λ λλ₯Ό ν΅ν΄ Callable μ μ€ννμ¬ κ·Έ κ²°κ³Ό κ°μ λ°ννλ€. μ΄ λ λ°ννλ κ°μ Future λ₯Ό ν΅ν΄ λ°μ μ μλ€.
Future<String> future = es.submit(() -> {
Thread.sleep(2000);
log.info("Async");
return "Hello";
});
Future
π₯ λ―Έλμ μ€νλ λΉλκΈ° μμ μ ν΅ν κ²°κ³Όμ λν νΈλ€λ¬(Hanlder)
get()
π Callable μ μμ κ²°κ³Όλ₯Ό λ°ννλ λ©μλ. ν΄λΉ λ©μλ μ€ν μ μ€λ λ μ€νμ κ²°κ³Ό κ°μ΄ λ°ν ν λ κΉμ§ λΈλ‘νΉ(Blocking) μνλ₯Ό μ μ§νλ€.
π€ 맀κ°λ³μλ₯Ό ν΅ν΄ Timeout μ μ§μ νλ λ°©λ²λ μλ€.
// μμ
(Task) μμ
Future<String> future = es.submit(() -> {
Thread.sleep(2000);
log.info("Async");
return "Hello";
});
//Blocking : κ²°κ³Όλ₯Ό λ°κΈ° μν΄ ν΄λΉ μ€λ λμμ κ²°κ³Ό κ°μ λ°κΈ° μ κΉμ§ λΈλ‘νΉμ νκ³ μλ€.
String hello = future.get();
System.out.println(hello);
log.info("Exit");
κ²°κ³Ό
07:45:20.340 [pool-1-thread-1] INFO asynchronous.toby.FutureEx - Async
Hello
07:45:20.346 [main] INFO asynchronous.toby.FutureEx - Exit
isDone()
π Callable μ μμ μ νμ¬ μνλ₯Ό λ°ννλ λ©μλμ΄λ€.
μμ μ΄ μλ£ λμλ€λ©΄ True λ₯Ό μμ§ μμ μ΄ μλ£λμ§ μμ μνλΌλ©΄ False λ₯Ό λ°ννλ€.
μ΄ λ©μλλ λ Ό λΈλ‘νΉ(Non-Blocking) μΌλ‘ μλνλ€.
Future<String> future = es.submit(() -> {
Thread.sleep(2000);
log.info("Async");
return "Hello";
});
System.out.println(future.isDone());
Thread.sleep(2100);
log.info("Exit");
System.out.println(future.get());
κ²°κ³Ό
08:01:03.626 [pool-1-thread-1] INFO asynchronous.toby.FutureEx - Async
true
08:01:07.736 [main] INFO asynchronous.toby.FutureEx - Exit
Hello
FutureTask
π₯ Future μ Callable(λΉλκΈ° μμ ) μ ν΅ν©ν κΈ°λ³Έ ꡬν체
done()
π FutureTask μ execute(λΉλκΈ° μμ )μ΄ λλ λ νΈμΆλλ Protected λ©μλλ‘ κ²°κ³Όλ₯Ό κ°λ‘μ±λ ννΉ(Hooking) μ΄ κ°λ₯νλ€.
FutureTask<String> future = new FutureTask<>(() -> {
Thread.sleep(2000);
log.info("Async");
return "Hello";
}) {
// λΉλκΈ° μμ
μ΄ λλ λ νΈμΆλλ λ©μλλ‘ ννΉμ΄ κ°λ₯νλ€.
@Override
protected void done() {
try {
System.out.println(get());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
};
es.execute(future);
es.shutdown();
κ²°κ³Ό
08:11:31.097 [pool-1-thread-1] INFO asynchronous.toby.FutureEx - Async
Hello
μ°Έμ‘°
'BackEnd > Java&Kotilin' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[Java] Java I/O (0) | 2022.10.01 |
---|---|
FutureTask λ₯Ό νμ©ν Non-Blocking μμ (0) | 2022.09.17 |
μΊ‘μνμ μ 보μλ (0) | 2022.08.28 |
[Spring Cloud] API Gatewayμ Filter μΆκ°νκΈ° (1) | 2022.08.27 |
API Gateway λ? with.Spring Cloud Gateway (0) | 2022.08.26 |
λκΈ
곡μ§μ¬ν
μ΅κ·Όμ μ¬λΌμ¨ κΈ
μ΅κ·Όμ λ¬λ¦° λκΈ
- Total
- Today
- Yesterday
λ§ν¬
TAG
- ATDD
- λΎ°μ‘±ν¨
- λ΄λΆ λ¨νΈν
- μν λΆν
- Object Pool
- μμ§ λΆν
- Session
- μ‘κ°ν μν€ν μ²
- λμ λμ€ν¨μΉ
- λ©λͺ¨λ¦¬ λ¨νΈν
- RestAssured
- λ©λͺ¨λ¦¬ ννΈν
- Clean Architecture
- κ°μ²΄ ν
- Sticky Session
- ν΄λ¦° μν€ν μ²
- μ»΄ν¬μ§ ν¨ν΄
- pass by value
- SpringBoot 2.2
- μΈλΆ λ¨νΈν
- pass by reference
- μΈμ λΆμΌμΉ
- μ μ νμ μΈμ΄
- λμ νμ μΈμ΄
- μ₯μ ν΄κ²°κΈ°
- multimap
- Memory Fragmentation
- java
- pool
- OOP
μΌ | μ | ν | μ | λͺ© | κΈ | ν |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
κΈ λ³΄κ΄ν¨