2010. 6. 29. 16:28
가끔 동일한 작업을 반복적으로 수행할 때 ExecutorService 를 사용하면 순차적으로 수행하는 것 보다 낳은 성능을 볼 수 있을 것임.
또한 계산 모듈이 종료되고 나온 결과값으로 다음 작업을 수행할 때 invokeAll() 은 유용할듯.
1. InvokeAllTest.java
2. WorkerTask.java
또한 계산 모듈이 종료되고 나온 결과값으로 다음 작업을 수행할 때 invokeAll() 은 유용할듯.
1. InvokeAllTest.java
package study.concurrency.invoke;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class InvokeAllTest {
final ExecutorService executorService = Executors.newFixedThreadPool(5);
final List<WorkerTask> list = new ArrayList<WorkerTask>();
public void makeTaskList() {
for(int i = 0;i<10;i++) {
list.add(new WorkerTask());
}
}
public void test() {
try {
List<Future> futureList = executorService.invokeAll((Collection)list);
try {
for(Future future : futureList ) {
System.out.println(future.get());
}
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
executorService.shutdown();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Completed");
}
public static void main(String[] args) {
new Launcher().test();
}
}
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class InvokeAllTest {
final ExecutorService executorService = Executors.newFixedThreadPool(5);
final List<WorkerTask> list = new ArrayList<WorkerTask>();
public void makeTaskList() {
for(int i = 0;i<10;i++) {
list.add(new WorkerTask());
}
}
public void test() {
try {
List<Future> futureList = executorService.invokeAll((Collection)list);
try {
for(Future future : futureList ) {
System.out.println(future.get());
}
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
executorService.shutdown();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Completed");
}
public static void main(String[] args) {
new Launcher().test();
}
}
2. WorkerTask.java
package study.concurrency.invoke;
import java.util.concurrent.Callable;
public class WorkerTask implements Callable<String> {
public String call() throws Exception {
// TODO Auto-generated method stub
for(int i = 0;i<5;i++) {
System.out.println(Thread.currentThread().getName() + ":" + "running...");
Thread.sleep(500);
}
return Thread.currentThread().getName() + ":" + "success";
}
}
import java.util.concurrent.Callable;
public class WorkerTask implements Callable<String> {
public String call() throws Exception {
// TODO Auto-generated method stub
for(int i = 0;i<5;i++) {
System.out.println(Thread.currentThread().getName() + ":" + "running...");
Thread.sleep(500);
}
return Thread.currentThread().getName() + ":" + "success";
}
}
'java > concurrency' 카테고리의 다른 글
[Concurrency] - LOCK(락) - ReentrantReadWriteLock(2) (0) | 2010.06.29 |
---|---|
[Concurrency] - LOCK(락) - ReentrantReadWriteLock(1) (0) | 2010.06.29 |
[Concurrency] - ThreadPoolExecutor 를 이용한 SimpleServer 만들기 (0) | 2010.06.29 |
[Concurrency] - ThreadPoolExecutor 를 이용한 Thread 동작 시간 구하기 (1) | 2010.06.29 |
[Concurrency] - shutdown hook 사용하기 (0) | 2010.06.29 |