import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import org.apache.commons.httpclient.HttpClient;
public class TestHttpClients2 implements Callable{
private String sResult;
private String userId;
public TestHttpClients2(String userId) {
this.userId = userId;
}
public String call() throws Exception {
//logic
Thread.sleep(1000);
return sResult;
}
public static void main(String[] args) {
ExecutorService pool = Executors.newFixedThreadPool(3);
Set> set = new HashSet >();
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(new File("C:\\xxx\\xxx.txt")));
while (br.ready()) {
Callablecallable = new TestHttpClients2(String.valueOf(br.readLine()));
Futurefuture = pool.submit(callable);
set.add(future);
}
for (Futurefuture : set) {
System.out.println("future=" + future.get());
System.out.println("future isDone=" + future.isDone());
}
//logic
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
}
ExecutorService, Callable
謝小胖老師指導
0 意見:
張貼留言