Callable多線程-創(chuàng)新互聯(lián)

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{
    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í)行
        Futuresubmit1 = executorService.submit(t1);
        Futuresubmit2 = executorService.submit(t2);
        Futuresubmit3 = 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ǎng)站設(shè)計與南部網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設(shè)計與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:做網(wǎng)站、網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣、域名申請、雅安服務(wù)器托管、企業(yè)郵箱。業(yè)務(wù)覆蓋南部地區(qū)。

你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧

新聞標(biāo)題:Callable多線程-創(chuàng)新互聯(lián)
分享路徑:http://bm7419.com/article0/gjsoo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、營銷型網(wǎng)站建設(shè)、做網(wǎng)站、網(wǎng)站排名、定制開發(fā)、軟件開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)