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()) { Callable callable = new TestHttpClients2(String.valueOf(br.readLine())); Future future = pool.submit(callable); set.add(future); } for (Future future : 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(); } } } }
2010年1月11日 星期一
ExecutorService, Callable
謝小胖老師指導
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言