Thread 간단하게 사용하기

간단한 Thread 사용

?
Runnable runnable = new Runnable() {
    try {
        String threadName = Thread.currentThread().getName();
        [[처리 로직]]
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
};
 
Thread thread = new Thread(runnable);
thread.start();


Thread 주요 함수




정해진 수의 Thread만 사용하는 ThreadPool

?
ExecutorService threadPool = Executors.newFixedThreadPool(
        // CPU 코어의 수만큼 최대 스레드를 사용하는 스레드풀을 생성
        Runtime.getRuntime().availableProcessors()
        );
 
Future future = threadPool.submit(new Runnable() {
    @Override
    public void run() {
        [[처리 로직 또는 다른 메서드 호출]]
    }
});
 
future.get();
 
// 작업 큐에 대기하고 있는 모든 작업을 처리한 뒤에 스레드풀을 종료
executorService.shutdown();


최대 최소 Thread 수, 일정 시간 이상 노는 Thread는 자동으로 제거하는 ThreadPool

?
ExecutorService threadPool = new ThreadPoolExecutor(
    3,      // 코어 스레드 개수
    100,    // 최대 스레드 개수
    120L,   // 놀고 있는 시간
    TimeUnit.SECONDS,   // 놀고 있는 시간 단위
    new SynchronousQueue()    // 작업 큐
);
 
Future future = threadPool.submit(new Runnable() {
    @Override
    public void run() {
        [[처리 로직 또는 다른 메서드 호출]]
    }
});
 
future.get();
 
// 작업 큐에 대기하고 있는 모든 작업을 처리한 뒤에 스레드풀을 종료
executorService.shutdown();

댓글

이 블로그의 인기 게시물

파일처리(한번에 모두읽기, 라인단위로 읽기, 쓰기, 리스트처리, 특정길이만큼 읽기)

math 함수 쓰기

AWS 가용성,확장성