当前位置:Gxl网 > 互联网 > 图片下载的两种方式Thread和Callable

图片下载的两种方式Thread和Callable

时间:2021-07-01 10:21:17 帮助过:2人阅读

图片下载

//练习Thread,实现多线程同步下载图片
public class ThreadTest2 extends Thread{
    private String url;
    private String name;

    public ThreadTest2(String url,String name){
        this.url=url;//路径
        this.name=name;//名字
    }
    //下载图片的执行体
    @Override
    public void run() {
        WebDownloader webDownloader=new WebDownloader();
        webDownloader.downloader(url,name);
        System.out.println("下载了文件名为:"+name);
    }

    public static void main(String[] args) {
        ThreadTest2 t1=new ThreadTest2("https://img2020.cnblogs.com/blog/2186812/202012/2186812-20201228112126269-922198589.jpg","1.jpg");
        ThreadTest2 t2=new ThreadTest2("https://img2020.cnblogs.com/blog/2186812/202012/2186812-20201228112131743-1242968120.jpg","2.jpg");
        ThreadTest2 t3=new ThreadTest2("https://img2020.cnblogs.com/blog/2186812/202012/2186812-20201228112139931-180392048.jpg","3.jpg");
        t1.start();
        t2.start();
        t3.start();
    }
}
//下载器
class WebDownloader{
    //下载方法
    public void downloader(String url,String name){
        try {
            FileUtils.copyURLToFile(new URL(url), new File(name));
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("IO异常");
        }
    }

Callable

//创建TestCallable类并继承Callable
public class TestCallable implements Callable {
    private String url;//图片路径
    private String name;//图片名字

    public TestCallable(String url,String name){
        this.url=url;
        this.name=name;
    }
    //下载图片的执行体
    @Override
    public Boolean call() {
        WebDownloader webDownloader=new WebDownloader();
        webDownloader.downloader(url,name);
        System.out.println("下载了文件名为:"+name);
        return true;
    }
    public static void main(String[] args) throws Exception{
        TestCallable t1=new TestCallable("https://img2020.cnblogs.com/blog/2186812/202012/2186812-20201228112126269-922198589.jpg","1.jpg");
        TestCallable t2=new TestCallable("https://img2020.cnblogs.com/blog/2186812/202012/2186812-20201228112131743-1242968120.jpg","2.jpg");
        TestCallable t3=new TestCallable("https://img2020.cnblogs.com/blog/2186812/202012/2186812-20201228112139931-180392048.jpg","3.jpg");		
        //开启执行服务		      new一个线程池
        ExecutorService es= Executors.newFixedThreadPool(3);//3代表3个线程
        //提交执行-------t1 t2 t3表示创建的对象
        Future f1=es.submit(t1);
        Future f2=es.submit(t2);
        Future f3=es.submit(t3);
        //获取结果
        boolean b1=f1.get();
        boolean b2=f2.get();
        boolean b3=f3.get();
        //关闭服务
        es.shutdownNow();
   }
}
//下载器
class WebDownloader{
    //下载方法
    public void downloader(String url,String name){
        try {
            FileUtils.copyURLToFile(new URL(url), new File(name));
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("IO异常");
        }
    }
}