CompletionService相当于Executor加上BlockingQueue,使用场景为当子线程并发了一系列的任务以后,主线程需要实时地取回子线程任务的返回值并同时顺序地处理这些返回值,谁先返回就先处理谁。下面两个类的工作效果相同,一个使用了CompletionService,代码更简捷些,一个直接使用的Executort和BlockingQueue,更复杂一些。其实读CompletionService的源代码,可以发现它内部其实就是对Executor和BlockingQueue的一个封装,不过封装后确定很优雅,用起来很方便。
[b]1.TestCompletionService.java[/b]
package test;
import java.io.File;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class TestCompletionService {
private static final ExecutorService NEW_FIXED_THREAD_POOL = Executors.newFixedThreadPool(10);
private static final File[] in = { new File("in1"),
new File("in2"), new File("in3"), new File("in4"), new File("in5") };
private static File out = new File("out");
private static CompletionService<File> completionService = new ExecutorCompletionService<File>(
NEW_FIXED_THREAD_POOL);
private static class DownLoader<File> implements Callable<File> {
private int index;
public void setIndex(int index) {
this.index = index;
}
@Override
public File call() throws Exception {
try {
Thread.sleep(index * 10000);
System.out.println("sleet [" + index * 10000 + "];");
} catch (InterruptedException e) {
System.out.println(e);
}
System.out.println("put file [" + in[index] + "];" + "time ["
+ System.currentTimeMillis() + "];");
return (File) in[index];
}
}
/**
* @param args
*/
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
DownLoader<File> downLoader = new DownLoader<File>();
downLoader.setIndex(i);
completionService.submit(downLoader);
}
for (int i = 0; i < 5; i++) {
try {
Future<File> future = completionService.take();
File file = future.get();
System.out.println("take file [" + file + "];" + "time ["
+ System.currentTimeMillis() + "];");
} catch (InterruptedException e) {
System.out.println(e);
} catch (ExecutionException e) {
System.out.println(e);
}
}
NEW_FIXED_THREAD_POOL.shutdown();
System.out.println("ok");
}
}
[b]
2.TestBlockingQueue.java[/b]
package test;
import java.io.File;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class TestBlockingQueue {
private static File[] in = { new File("in1"), new File("in2"),
new File("in3"), new File("in4"), new File("in5") };
private static File out = new File("out");
private static ExecutorService executorService = Executors.newFixedThreadPool(10);
private static class DownLoader implements Runnable {
private int index;
private BlockingQueue<File> blockingQueue;
public void setBlockingQueue(BlockingQueue<File> blockingQueue) {
this.blockingQueue = blockingQueue;
}
public void setIndex(int index) {
this.index = index;
}
@Override
public void run() {
try {
Thread.sleep(index * 10000);
System.out.println("sleet [" + index * 10000 + "];");
} catch (InterruptedException e) {
System.out.println(e);
}
if (index == 3) {
}
try {
System.out.println("put file [" + in[index] + "];" + "time ["
+ System.currentTimeMillis() + "];");
blockingQueue.put(in[index]);
} catch (InterruptedException e) {
System.out.println(e);
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
BlockingQueue<File> blockingQueue = new ArrayBlockingQueue<File>(10);
for (int i = 0; i < 5; i++) {
DownLoader downLoader = new DownLoader();
downLoader.setBlockingQueue(blockingQueue);
downLoader.setIndex(i);
executorService.execute(downLoader);
}
for (int i = 0; i < 5; i++) {
try {
File file = blockingQueue.poll(8, TimeUnit.SECONDS);
System.out.println("take file [" + file + "];" + "time ["
+ System.currentTimeMillis() + "];");
} catch (InterruptedException e) {
System.out.println(e);
}
}
executorService.shutdown();
System.out.println("ok");
}
}
[b]1.TestCompletionService.java[/b]
package test;
import java.io.File;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class TestCompletionService {
private static final ExecutorService NEW_FIXED_THREAD_POOL = Executors.newFixedThreadPool(10);
private static final File[] in = { new File("in1"),
new File("in2"), new File("in3"), new File("in4"), new File("in5") };
private static File out = new File("out");
private static CompletionService<File> completionService = new ExecutorCompletionService<File>(
NEW_FIXED_THREAD_POOL);
private static class DownLoader<File> implements Callable<File> {
private int index;
public void setIndex(int index) {
this.index = index;
}
@Override
public File call() throws Exception {
try {
Thread.sleep(index * 10000);
System.out.println("sleet [" + index * 10000 + "];");
} catch (InterruptedException e) {
System.out.println(e);
}
System.out.println("put file [" + in[index] + "];" + "time ["
+ System.currentTimeMillis() + "];");
return (File) in[index];
}
}
/**
* @param args
*/
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
DownLoader<File> downLoader = new DownLoader<File>();
downLoader.setIndex(i);
completionService.submit(downLoader);
}
for (int i = 0; i < 5; i++) {
try {
Future<File> future = completionService.take();
File file = future.get();
System.out.println("take file [" + file + "];" + "time ["
+ System.currentTimeMillis() + "];");
} catch (InterruptedException e) {
System.out.println(e);
} catch (ExecutionException e) {
System.out.println(e);
}
}
NEW_FIXED_THREAD_POOL.shutdown();
System.out.println("ok");
}
}
[b]
2.TestBlockingQueue.java[/b]
package test;
import java.io.File;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class TestBlockingQueue {
private static File[] in = { new File("in1"), new File("in2"),
new File("in3"), new File("in4"), new File("in5") };
private static File out = new File("out");
private static ExecutorService executorService = Executors.newFixedThreadPool(10);
private static class DownLoader implements Runnable {
private int index;
private BlockingQueue<File> blockingQueue;
public void setBlockingQueue(BlockingQueue<File> blockingQueue) {
this.blockingQueue = blockingQueue;
}
public void setIndex(int index) {
this.index = index;
}
@Override
public void run() {
try {
Thread.sleep(index * 10000);
System.out.println("sleet [" + index * 10000 + "];");
} catch (InterruptedException e) {
System.out.println(e);
}
if (index == 3) {
}
try {
System.out.println("put file [" + in[index] + "];" + "time ["
+ System.currentTimeMillis() + "];");
blockingQueue.put(in[index]);
} catch (InterruptedException e) {
System.out.println(e);
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
BlockingQueue<File> blockingQueue = new ArrayBlockingQueue<File>(10);
for (int i = 0; i < 5; i++) {
DownLoader downLoader = new DownLoader();
downLoader.setBlockingQueue(blockingQueue);
downLoader.setIndex(i);
executorService.execute(downLoader);
}
for (int i = 0; i < 5; i++) {
try {
File file = blockingQueue.poll(8, TimeUnit.SECONDS);
System.out.println("take file [" + file + "];" + "time ["
+ System.currentTimeMillis() + "];");
} catch (InterruptedException e) {
System.out.println(e);
}
}
executorService.shutdown();
System.out.println("ok");
}
}