2010. 6. 29. 16:05
이번 예제도 "Excutor를 사용한 thread pool 샘플" 을 토대로 작성됨
Executor 를 상속받은 ExecutorService 를 사용하여 pool 의 동작 주기를 관리한다.
void stop(..) 함수는 java api 를 참조함..
Executor 를 상속받은 ExecutorService 를 사용하여 pool 의 동작 주기를 관리한다.
void stop(..) 함수는 java api 를 참조함..
1. ConsumerThread.java 수정
package study.concurrrency;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.TimeUnit;
public class ConsumerThread implements Runnable {
Thread thread;
private static final ExecutorService pool = Executors.newFixedThreadPool(10);
public void start() {
thread = new Thread(this);
thread.start();
}
// java api 참조
void stop(ExecutorService pool) {
pool.shutdown(); // Disable new tasks from being submitted
try {
// Wait a while for existing tasks to terminate
if (! pool.awaitTermination(60, TimeUnit.SECONDS)) {
pool.shutdownNow(); // Cancel currently executing tasks
// Wait a while for tasks to respond to being cancelled
if (! pool.awaitTermination(60, TimeUnit.SECONDS))
System.err.println("Pool did not terminate");
}
} catch (InterruptedException ie) {
// (Re-) Cancel if current thread also interrupted
pool.shutdownNow();
// Preserve interrupt status
Thread.currentThread().interrupt();
}
}
@Override
public void run() {
// TODO Auto-generated method stub
while(!pool.isShutdown()) {
try {
pool.execute(new Worker(BlockingQueueSample.getInstance().getData()));
} catch(RejectedExecutionException e) {
if(!pool.isShutdown()) {
System.out.println("작업이 거부당했구만..");
}
}
}
}
}
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.TimeUnit;
public class ConsumerThread implements Runnable {
Thread thread;
private static final ExecutorService pool = Executors.newFixedThreadPool(10);
public void start() {
thread = new Thread(this);
thread.start();
}
// java api 참조
void stop(ExecutorService pool) {
pool.shutdown(); // Disable new tasks from being submitted
try {
// Wait a while for existing tasks to terminate
if (! pool.awaitTermination(60, TimeUnit.SECONDS)) {
pool.shutdownNow(); // Cancel currently executing tasks
// Wait a while for tasks to respond to being cancelled
if (! pool.awaitTermination(60, TimeUnit.SECONDS))
System.err.println("Pool did not terminate");
}
} catch (InterruptedException ie) {
// (Re-) Cancel if current thread also interrupted
pool.shutdownNow();
// Preserve interrupt status
Thread.currentThread().interrupt();
}
}
@Override
public void run() {
// TODO Auto-generated method stub
while(!pool.isShutdown()) {
try {
pool.execute(new Worker(BlockingQueueSample.getInstance().getData()));
} catch(RejectedExecutionException e) {
if(!pool.isShutdown()) {
System.out.println("작업이 거부당했구만..");
}
}
}
}
}
'java > concurrency' 카테고리의 다른 글
[Concurrency] - Future 를 이용한 작업종료 (0) | 2010.06.29 |
---|---|
[Concurrency] - interrupt 사용 (0) | 2010.06.29 |
[Concurrency] - Executor 를 사용한 thread pool 샘플 (0) | 2010.06.29 |
[Concurrency] - BlockingQueue 과 producer-consumer 패턴 활용법 (0) | 2010.06.29 |
[Concurrency] - Singleton Pattern with java (0) | 2010.06.29 |