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

图片下载

//练习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<Boolean> {
    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<Boolean> f1=es.submit(t1);
        Future<Boolean> f2=es.submit(t2);
        Future<Boolean> 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异常");
        }
    }
}
posted @ 2020-12-29 10:19  6Ocean  阅读(96)  评论(0编辑  收藏  举报