Concurrent Package - springscomin/be-was GitHub Wiki

Concurrent νŒ¨ν‚€μ§€

λ™μ‹œμ„± ν”„λ‘œκ·Έλž˜λ°μ— μœ μš©ν•œ Utility Class νŒ¨ν‚€μ§€.

  1. 곡유 μžμ›μ— λŒ€ν•œ λ°μ΄ν„°μ˜ 일관성과 μ›μžμ μΈ 연산을 μ§€μ›ν•˜λŠ” 데이터 νƒ€μž…, μ»¬λž™μ…˜ 클래슀

    β‡’ 곡유 데이터에 λŒ€ν•œ 경쟁 μƒν™©μ—μ„œ μ•ˆμ „ν•œ μž‘μ—…μ„ 보μž₯ν•˜κΈ° μœ„ν•΄ μ‚¬μš©

    • AtomicInteger, AtomicLong λ“±
    • concurrentHashMap λ“±
  2. Queues: thread-safe, non-blocking, ν™•μž₯ κ°€λŠ₯ν•œ Queue

  3. λͺ…μ‹œμ  락을 μ‚¬μš©ν•  수 μžˆλ„λ‘ ν•˜λŠ” 클래슀

  4. 닀쀑 μŠ€λ ˆλ“œ ν”„λ‘œκ·Έλž¨μ„ λ§Œλ“€λ•Œ μ‚¬μš©ν•  수 μžˆλŠ” μœ μš©ν•œ 클래슀

    (μŠ€λ ˆλ“œ 풀을 μ‚¬μš©ν•΄ μ—¬λŸ¬ μŠ€λ ˆλ“œλ₯Ό μ‹€ν–‰, μ‹œμž‘, 쀑단 등을 관리)

    Untitled

    • 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번째
    ...

    Runnable β†’ Return void

    비동기 μž‘μ—…μ„ μ‹€ν–‰ν•˜κΈ° μœ„ν•œ μΈν„°νŽ˜μ΄μŠ€

    β†’ λ³„λ„μ˜ μŠ€λ ˆλ“œμ—μ„œ 싀행될 μ½”λ“œλ₯Ό μ •μ˜ν•˜λŠ” 데 μ‚¬μš©.

    void run()- λ°˜ν™˜ νƒ€μž…μ΄ μ—†λŠ” 좔상 λ©”μ„œλ“œλ₯Ό κ°–λŠ” ν•¨μˆ˜ν˜• μΈν„°νŽ˜μ΄μŠ€

    Callable β†’ Return Reference

    비동기 μž‘μ—…μ„ μ‹€ν–‰ν•˜κΈ° μœ„ν•œ μΈν„°νŽ˜μ΄μŠ€

    β†’ λ³„λ„μ˜ μŠ€λ ˆλ“œμ—μ„œ 싀행될 μ½”λ“œλ₯Ό μ •μ˜ν•˜λŠ” 데 μ‚¬μš©.

    V call() throws Exception; - λ°˜ν™˜ νƒ€μž…μ΄ μžˆλŠ” μΆ”μƒλ©”μ„œλ“œλ₯Ό κ°–λŠ” ν•¨μˆ˜ν˜• μΈν„°νŽ˜μ΄μŠ€

    Future

    task의 μ‹€ν–‰ κ²°κ³Ό λ˜λŠ” task의 μƒνƒœλ₯Ό 확인할 수 μžˆλŠ” 클래슀

    β†’ Computation이 μ™„λ£Œ λ˜μ—ˆλŠ”μ§€ 확인 ν•  수 있음.

    • future.isDone(), future.isCancelled()

    β†’ Computation을 μ·¨μ†Œ ν•  수 있음.

    • future.cancel(true)

    β†’ λ―Έλž˜μ— μ™„λ£Œλ  Computation의 κ²°κ³Όλ₯Ό λ°›μ•„μ˜¬ 수 있음.

    • future.get()

      β†’ .get() λ©”μ„œλ“œλ₯Ό ν˜ΈμΆœν•˜λ©΄, taskκ°€ μ™„λ£Œλ  λ•ŒκΉŒμ§€ block μƒνƒœκ°€ 됨.

Reference

⚠️ **GitHub.com Fallback** ⚠️