Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- AssertJ
- 배열
- 자바스크립트
- class
- JS
- array
- 메소드
- SpringBoot
- Python
- thread
- c#
- docker
- JavaScript
- Swing
- Docker Desktop
- SSL
- GIT
- Dict
- 객체
- synchronized
- StringBuilder
- 파이썬
- Java
- 스프링부트
- 저장소
- 클래스
- event
- 프로그래머스스쿨
- 자바
- join()
Archives
- Today
- Total
정리노트
[자바/java] Thread. ThreadPool 본문
thread의 개수보다 작업의 개수가 더 많은 경우에는 작업은 FIFO Queue에서 대기한다.
Runnabel 객체를 구현하고 ThreadPoolExecutor로 보내기만 하면 됨. (ThreadPoolExecutor 는 thread로 실행하는 담당)
thread가 새로 생성되는 시간을 절약하며 ThreadPoolExecutor는 thread가 계속 생성되는 것을 방지하기 위하여
몇 개의 thread를 미리 생성한 후에 pooling하여 사용한다.
package threadPoolTest;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
public class ThreadPoolTest implements Runnable {
private String name;
public ThreadPoolTest(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void run() {
try {
System.out.println("실행중: " + this.name);
Thread.sleep((long)(Math.random() * 1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(2); // thread-pool의 크기 = 2
for (int i = 1; i <= 5; i++) {
ThreadPoolTest task = new ThreadPoolTest("작업: " + i);
System.out.println("작업생성: " + task.getName());
executor.execute(task);
}
executor.shutdown(); // shutdown() : 현재 처리중인 작업, 작업 큐에 대기하는 모든 작업을 완료한 뒤 종료
}
}
728x90
'프로그래밍 > Java' 카테고리의 다른 글
[자바/java] thread 간 동작조정(생산자, 소비자) (0) | 2023.02.04 |
---|---|
[자바/java] Thread 동기화 synchronized (0) | 2023.02.04 |
[자바/java] Thread. interrupt()로 컨트롤 (0) | 2023.02.03 |
[자바/java] Thread . sleep(), join() 사용법기초 (0) | 2023.02.03 |
[자바/java] text파일에 저장된 단어로 행맨만들기 (0) | 2023.02.01 |