HomeAboutMeBlogGuest
© 2025 Sejin Cha. All rights reserved.
Built with Next.js, deployed on Vercel
📝
남득윤 학습 저장소
/
🧵
멀티쓰레드, 동시성 프로그래밍
/
🧵
Java Concurrency and Multithreding
/
Java ExecutorService -Part 2

Java ExecutorService -Part 2

Shut down ExecutorService

void shutdown( )
  • submit 된 모든 task 들이 완료 될때 까지 대기 후 종료
 
List<Runnable> shutdownNow( )
  • submit 된 task 중 남은 것을 리턴하고 강제 종료
 
void awaitTermination(Long time, Timeunit timeunit)
  • The ExecutorService awaitTermination() method will block the thread calling it until either the ExecutorService has shutdown completely, or until a given time out occurs. The awaitTermination() method is typically called after calling shutdown() or shutdownNow(). Here is an example of calling ExecutorService awaitTermination():
    executorService.shutdown(); executorService.awaitTermination(10_000L, TimeUnit.MILLISECONDS );
     
     

    Cancel task via Future

    public class ExecutorServiceExample8 { public static void main(String[] args) { ExecutorService es = Executors.newFixedThreadPool(1); Future<String> future = es.submit(newCallable("Task 1.1")); System.out.println(future.isDone()); boolean mayInterrupt = true; future.cancel(mayInterrupt); String result = null; try { result = future.get(); System.out.println("result = " + result); } catch (InterruptedException e) { } catch (ExecutionException e) { } catch (CancellationException e){ System.out.println("Cannot call Future.get() since task was cancelled"); } System.out.println(future.isDone()); es.shutdown(); } }
     

    Ideal number of threads in ExecutorService

    notion image