| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 | 31 |
- MySQL
- boot
- java
- Web
- 스프링
- java8
- 인터페이스
- Git
- 엘라스틱서치
- RCP
- Spring Boot
- elasticsearch
- 독후감
- 자바스크립트
- node
- kibana
- javascript
- nodejs
- error
- Spring
- 맛집
- 백준
- 알고리즘
- JPA
- jface
- 자바
- 리뷰
- effective
- 이펙티브
- 후기
- Today
- Total
wedul
java thread pool 소개 본문
스레드 풀
Public class GzipRunnable implements Runnable {
Private final File input;
Public GZipRunnable(File input) {
This.input = intpu;
}
@Override
Pulbic void run(){
if (!input.getName().endsWith(".gz")) {
File output = new File(input.getParent(), input.getName() + ".gz");
If(!output.exists) {
Try (
InputStream in = new BufferedInputStream(new FileInputStream(input));
OutPutStream out = new BufferedOutputStream(
New GZIPOutputStream(new FileOutputStream(output)));
) {
Int b;
While ((b = in.read()) != -1) out.write(b);
Out.flush();
} catch (IOException ex){
}
}
}
위에 소스는 입력, 출력 스트림 모두 try 블록의 시작에서 선언되었고 try 블록 끝에서 자동으로 닫힌다. 또한 입력과 출력 모두에 사용된 버퍼링은 입출력이 많이 발생하는 부분에서 문제가 될 수 있다.
그래서 4개의 스레드 풀로 해당 스레드를 처리할 예정이다.
스레드풀을 이용한 스레드 처리 방식은 다음과 같다.
Public class GzipAllFiles {
Public final static int THREAD_COUNT = 4;
Public static void main(String args[]) {
ExecutorService pool = Executors.newFixedThreadPool(THREAD_COUNT);
For(String filename: args){
File f = new File(filename);
If (f.exist()) {
If (f.isDirectory()) {
File[] files = f.listFiles();
For (int I = 0; i< files.length; i++) {
If(!files[i].isDirectory()) {
Runnable task = new GZipRunnable(file[i]);
Pool.submit(task);
}
}
} else {
Runnable task = new GZipRunnable(f);
Pool.sublit(task);
}
}
}
Pool.shutdown();
}
}
Shutdown()은 진행중인 작업을 멈추는 것이 아닌 단순히 추가적으로 입력할 스레드가 없다는 의미이다.
즉시 멈추기 위해서는 shutdownNow() 메소드를 호출 할 수 있다.
'JAVA > Thread' 카테고리의 다른 글
| Thread wait(), notify() 소개 (0) | 2016.12.21 |
|---|---|
| Thread 크리티컬 세션 (0) | 2016.12.21 |
| JAVA 스레드 스케줄링 (0) | 2016.12.21 |
| Thread 동기화 문제 (0) | 2016.12.21 |
| JAVA Thread Futher, Callable, Executor (0) | 2016.12.21 |
