package com.ljy.test; import org.apache.commons.io.FileUtils; import java.io.File; import java.io.IOException; import java.net.URL; import java.util.concurrent.*; public class TestDownLoader1 implements Callable站在用戶的角度思考問題,與客戶深入溝通,找到南部網(wǎng)站設(shè)計(jì)與南部網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:做網(wǎng)站、網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、域名申請(qǐng)、雅安服務(wù)器托管、企業(yè)郵箱。業(yè)務(wù)覆蓋南部地區(qū)。{ private String url; private String name; public TestDownLoader1(String url,String name){ this.url=url; this.name=name; } @Override public Boolean call() throws Exception { Downloader downloader = new Downloader(); downloader.download(url,name); System.out.println("文件名為:"+name); return true; } class Downloader{ public void download(String url,String name){ try { FileUtils.copyURLToFile(new URL(url),new File(name)); } catch (IOException e) { e.printStackTrace(); } } } public static void main(String[] args) { TestDownLoader1 t1 = new TestDownLoader1("https://img2.baidu.com/it/u=1975709945,2357807807&fm=253&fmt=auto&app=138&f=JPEG?w=499&h=332", "11.jpg"); TestDownLoader1 t2 = new TestDownLoader1("https://img2.baidu.com/it/u=3340436266,2054185400&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=418", "22.jpg"); TestDownLoader1 t3 = new TestDownLoader1("https://img2.baidu.com/it/u=2144825314,638245739&fm=253&fmt=auto&app=138&f=JPEG?w=499&h=281", "33.jpg"); //創(chuàng)建線程池服務(wù) ExecutorService executorService = Executors.newFixedThreadPool(3); //提交執(zhí)行 Future submit1 = executorService.submit(t1); Future submit2 = executorService.submit(t2); Future submit3 = executorService.submit(t3); //獲取結(jié)果 boolean rs1 = false; try { rs1 = submit1.get(); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } boolean rs2 = false; try { rs2 = submit2.get(); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } boolean rs3 = false; try { rs3 = submit3.get(); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } //打印返回值 System.out.println(rs1); System.out.println(rs2); System.out.println(rs3); //關(guān)閉服務(wù) executorService.shutdownNow(); } }
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購(gòu),新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧