2010. 6. 29. 16:11
인터럽트에 응답하니께. 작업을 할 수 있는데. 응답하지 못하는 놈들은?
뭐.. 우선은 Future 를 이용해서 작업을 중단해 보더라구..
1. Launcher.java
- newSingleThreadExecutor() 의 Executor 를 생성해서 사용함.
- Timeout Exception 이 발생하면 해당 잡업을 종료하는 무식한 넘임.
2. Task.java
- 별루 하는 일 없이 처먹다가 배부르면 안먹는다고 뻐팅기는 지극히 현실적이면서.. 무식한놈.
뭐.. 우선은 Future 를 이용해서 작업을 중단해 보더라구..
1. Launcher.java
- newSingleThreadExecutor() 의 Executor 를 생성해서 사용함.
- Timeout Exception 이 발생하면 해당 잡업을 종료하는 무식한 넘임.
package study.interrupt.future;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
public class Launcher {
private static final ExecutorService exce = Executors.newSingleThreadExecutor();
public static void main(String[] args) {
BlockingQueue<String> queue = new ArrayBlockingQueue<String>(10);
Future<?> task = exce.submit(new Task(queue));
try {
task.get(3, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
task.cancel(true);
}
if(task.isCancelled()) {
System.out.println("종료됨");
}
for(String str : queue) {
System.out.print(str + " : " );
}
}
}
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
public class Launcher {
private static final ExecutorService exce = Executors.newSingleThreadExecutor();
public static void main(String[] args) {
BlockingQueue<String> queue = new ArrayBlockingQueue<String>(10);
Future<?> task = exce.submit(new Task(queue));
try {
task.get(3, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
task.cancel(true);
}
if(task.isCancelled()) {
System.out.println("종료됨");
}
for(String str : queue) {
System.out.print(str + " : " );
}
}
}
2. Task.java
- 별루 하는 일 없이 처먹다가 배부르면 안먹는다고 뻐팅기는 지극히 현실적이면서.. 무식한놈.
package study.interrupt.future;
import java.util.concurrent.BlockingQueue;
public class Task implements Runnable {
private final BlockingQueue<String> queue;
Task(BlockingQueue<String> queue) {
this.queue = queue;
}
@Override
public void run() {
try {
int index = 0;
while(!Thread.currentThread().isInterrupted()) {
queue.put("" + index);
System.out.print(index + " : ");
index ++;
Thread.sleep(50);
}
} catch(InterruptedException e) {
e.printStackTrace();
}
}
}
import java.util.concurrent.BlockingQueue;
public class Task implements Runnable {
private final BlockingQueue<String> queue;
Task(BlockingQueue<String> queue) {
this.queue = queue;
}
@Override
public void run() {
try {
int index = 0;
while(!Thread.currentThread().isInterrupted()) {
queue.put("" + index);
System.out.print(index + " : ");
index ++;
Thread.sleep(50);
}
} catch(InterruptedException e) {
e.printStackTrace();
}
}
}
'java > concurrency' 카테고리의 다른 글
[Concurrency] - ThreadPoolExecutor 를 이용한 Thread 동작 시간 구하기 (1) | 2010.06.29 |
---|---|
[Concurrency] - shutdown hook 사용하기 (0) | 2010.06.29 |
[Concurrency] - interrupt 사용 (0) | 2010.06.29 |
[Concurrency] - ExecutorService 를 이용한 동작주기 예제 (0) | 2010.06.29 |
[Concurrency] - Executor 를 사용한 thread pool 샘플 (0) | 2010.06.29 |