• 图片下载的两种方式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异常");
            }
        }
    }
    
  • 相关阅读:
    【草稿】自定义ASP.NET MVC Html辅助方法
    Python安装(64位Win8.1专业版)
    部署WP程序到自己的手机
    C++中vector小学习,顺便查了下<stdio.h>(或<cstdio>)
    关于ENVI5.0菜单栏不能正常显示(win7 x86系统)
    如何让一个精灵跟随触点移动
    GDAL在VS下配置测试
    【docker】修改现有容器的端口
    Django channles线上部署(腾讯云)
    【leafletjs】添加标记、轨迹线与删除标记、轨迹线
  • 原文地址:https://www.cnblogs.com/lhy8116/p/14205111.html
Copyright © 2020-2023  润新知