廖雪峰Java11多线程编程-3高级concurrent包-7Future

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

class DownloadTask implements Callable<String> {
    String url;
    public DownloadTask(String url){
        this.url = url;
    }
    public String call() throws Exception {
        System.out.println("start download " + url + "...");
        URLConnection conn = new URL(this.url).openConnection();
        conn.connect();
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(conn))){//有问题

            String s = null;
            StringBuilder sb = new StringBuilder();
            while ((s = reader.readLine()) != null) {
                sb.append("\n");
            }
            return sb.toString();
        }

    }

}
public class Main{
    public static void main(String[] args) throws Exception{
        ExecutorService executor = Executors.newFixedThreadPool(3);
        DownloadTask task = new DownloadTask("https://www.sina.com.cn");
        Future<String> future = executor.submit(task);
        String html = future.get();
        System.out.println(html);
        executor.shutdown();
    }
}
上一篇:java concurrent并发包使用


下一篇:java并发计算的几种基本使用示例