Concurrent Package - springscomin/be-was GitHub Wiki
λμμ± νλ‘κ·Έλλ°μ μ μ©ν Utility Class ν¨ν€μ§.
-
곡μ μμμ λν λ°μ΄ν°μ μΌκ΄μ±κ³Ό μμμ μΈ μ°μ°μ μ§μνλ λ°μ΄ν° νμ , 컬λμ ν΄λμ€
β 곡μ λ°μ΄ν°μ λν κ²½μ μν©μμ μμ ν μμ μ 보μ₯νκΈ° μν΄ μ¬μ©
-
AtomicInteger,AtomicLongλ± - concurrentHashMap λ±
-
-
Queues: thread-safe, non-blocking, νμ₯ κ°λ₯ν Queue
-
λͺ μμ λ½μ μ¬μ©ν μ μλλ‘ νλ ν΄λμ€
-
λ€μ€ μ€λ λ νλ‘κ·Έλ¨μ λ§λ€λ μ¬μ©ν μ μλ μ μ©ν ν΄λμ€
(μ€λ λ νμ μ¬μ©ν΄ μ¬λ¬ μ€λ λλ₯Ό μ€ν, μμ, μ€λ¨ λ±μ κ΄λ¦¬)
-
Executors
- λ€μν ExecutorService ꡬν κ°μ²΄μ ν©ν 리 λ©μλλ₯Ό μ 곡νλ ν΄λμ€
- ExecutorService ꡬν κ°μ²΄ μ’ λ₯ β ThreadPoolExecutor, ForkJoinPool β¦ λ±λ±
-
Executor μΈν°νμ΄μ€
Runnable μμ μ μ€ννλ 주체
-
void execute(Runnable command);- λ―Έλμ μ΄λ μμ μ ν¨μν μΈν°νμ΄μ€ Runnableμ ꡬνν ν΄λμ€ κ°μ²΄μ run λ©μλ μ€ν
- Executor ꡬνμ λ°λΌ, μ μ€λ λ, νλ§λ μ€λ λ λλ νΈμΆ μ€λ λμμ μ€νν μ μμ.
-
-
ExecutorService μΈν°νμ΄μ€
- μ£Όμ
-
void execute(Runnable command);- μμ λμΌ
-
<T> Future<T> submit(Callable<T> task);- Callable ν μ€ν¬λ₯Ό μ€ννκ³ , taskκ° λ°νν Future κ°μ²΄λ₯Ό μ λ¬ λ°μ.
-
<T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)- μ£Όμ΄μ§ ν μ€ν¬λ€μ λͺ¨λ μ²λ¦¬νκ³ , μ±κ³΅μ μΌλ‘ μ²λ¦¬λ μμ λ€μ΄ λ°νν Future Listλ₯Ό λ°μμ€λ λ©μλ
- λͺ¨λ κ²°κ³Όκ° λμ¬ λ κΉμ§ λκΈ° - λΈλ‘νΉ λ°©μ
-
<T> T invokeAny(Collection<? extends Callable<T>> tasks)- μ¬λ¬ μμ μ μ²λ¦¬νκ³ , κ°μ₯ λ¨Όμ λ°νλ κ°μ λ°μμ€λ λ©μλ
- ...
-
- κΈ°ν
-
void shutdown(): μ€νμ€μΈ μμ κΉμ§λ§ μ²λ¦¬, μλ‘μ΄ μμ λ°μ§ μμ. -
List<Runnable> shutdownNow(): μ€νμ€μΈ μμ λ€λ λͺ¨λ μ’ λ£ boolean isShutdown();boolean isTerminated();boolean awaitTermination(long timeout, TimeUnit unit)
-
- μ£Όμ
example
public class ExecutorTest { public static void main(String[] args) { Service service = new Service(); service.serveRunnable(); } static class Service { public void serveRunnable() { ExecutorService executorService = Executors.newFixedThreadPool(10); System.out.println("μμ 보μ₯ X"); for (int i = 0; i < 100; i++) { int n = i; executorService.execute(() -> { System.out.println(n + "λ²μ§Έ"); }); } executorService.shutdown(); } } } ---- μμ 보μ₯ X 6λ²μ§Έ 10λ²μ§Έ 11λ²μ§Έ 2λ²μ§Έ 5λ²μ§Έ ...
λΉλκΈ° μμ μ μ€ννκΈ° μν μΈν°νμ΄μ€
β λ³λμ μ€λ λμμ μ€νλ μ½λλ₯Ό μ μνλ λ° μ¬μ©.
void run()- λ°ν νμ μ΄ μλ μΆμ λ©μλλ₯Ό κ°λ ν¨μν μΈν°νμ΄μ€λΉλκΈ° μμ μ μ€ννκΈ° μν μΈν°νμ΄μ€
β λ³λμ μ€λ λμμ μ€νλ μ½λλ₯Ό μ μνλ λ° μ¬μ©.
V call() throws Exception; - λ°ν νμ μ΄ μλ μΆμλ©μλλ₯Ό κ°λ ν¨μν μΈν°νμ΄μ€taskμ μ€ν κ²°κ³Ό λλ taskμ μνλ₯Ό νμΈν μ μλ ν΄λμ€
β Computationμ΄ μλ£ λμλμ§ νμΈ ν μ μμ.
-
future.isDone(),future.isCancelled()
β Computationμ μ·¨μ ν μ μμ.
future.cancel(true)
β λ―Έλμ μλ£λ Computationμ κ²°κ³Όλ₯Ό λ°μμ¬ μ μμ.
-
future.get()β .get() λ©μλλ₯Ό νΈμΆνλ©΄, taskκ° μλ£λ λκΉμ§ block μνκ° λ¨.
-
Reference
