zoukankan      html  css  js  c++  java
  • 图片下载的两种方式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异常");
            }
        }
    }
    
  • 相关阅读:
    20165223 week6测试错题总结
    20165223《Java程序设计》第七周Java学习总结
    20165207 第八周学习总结
    2017-2018-2 20165207实验二《Java面向对象程序设计》实验报告
    20165207 第七周学习总结
    20165207 第六周学习总结
    20165207 实验一 Java开发环境的熟悉
    20165207 第五周学习总结
    20165207 第四周学习总结
    20165207 第三周学习总结
  • 原文地址:https://www.cnblogs.com/lhy8116/p/14205111.html
Copyright © 2011-2022 走看看