JAVA初学(八):多线程下载多个网页文件

版权声明:文章为作者原创,若要转载请获得作者同意。尊重版权,从你我做起! https://blog.csdn.net/qq_37768971/article/details/87863846

一、题目:

下面的程序可以下载多个网页文件(download方法已写好),请将它改成多线程进行下载,如果可能, 显示计算全部下载完成程序所用的时间(提示:new Date().getTime()可以得到当前时间的毫秒数)。

import java.net.URL;
import java.io.*;
 
class Downloader 
{
    public static void main(String[] args)
        throws Exception
    {
        final URL[] urls = {
            new URL("https://www.pku.edu.cn"),
            new URL("https://www.baidu.com"),
            new URL("https://www.sina.com.cn"),
            new URL("https://www.dstang.com")
        };
        final String[] files = {
            "pku.htm", 
            "baidu.htm",
            "sina.htm", 
            "study.htm",
        };
 
        for(int idx=0; idx<urls.length; idx++){
            try{
                System.out.println( urls[idx] );
                download( urls[idx], files[idx]);
            }catch(Exception ex){
                ex.printStackTrace();
            }
        }
    }
    static void download( URL url, String file)
        throws IOException
    {
        try(InputStream input = url.openStream();
            OutputStream output = new FileOutputStream(file))
        {
            byte[] data = new byte[1024];
            int length;
            while((length=input.read(data))!=-1){
                output.write(data,0,length);
            }
        }
    }
}

二、程序编写(附注释)

/**************************************************************************************
* @多线程练习题
* @作者:IMUHERO
* @时间:2019/2/21
***************************************************************************************/
package Learning;
import java.net.URL;
import java.io.*;

/*
* 类名:Downloader
* 功能:实现多线程下载相应网页
* 完成时间:2019/2/21
* */
class Downloader extends Thread{
    URL url;String file;
     Downloader( URL url, String file){
         this.url=url;
         this.file=file;
         System.out.println( url);
     }

    public void run(){
        java.io.InputStream input = null ;
        java.io.OutputStream output = null ;
        try{

            input =  url.openStream();
            output = new FileOutputStream(file);
            byte[] data = new byte[1024];
            int length;
            try {
                while((length=input.read(data))!=-1){
                    output.write(data,0,length);
                }
            }catch (IOException e1) {
                e1.printStackTrace();
            }
        }catch(IOException e1){
            e1.printStackTrace();
        }

    }

}

class test
{
    public static void main(String[] args)
            throws Exception
    {
        final URL[] urls = {                                    //URL数组,定义网页地址
                new URL("https://www.pku.edu.cn"),
                new URL("https://www.baidu.com"),
                new URL("https://www.sina.com.cn"),
                new URL("https://www.dstang.com")
        };
        final String[] files = {                                //定义网页htm文件名
                "pku.htm",
                "baidu.htm",
                "sina.htm",
                "study.htm",
        };

        Downloader downloader1=new Downloader(urls[0],files[0]);        //启动四个线程
        downloader1.start();
        Downloader downloader2=new Downloader(urls[1],files[1]);
        downloader2.start();
        Downloader downloader3=new Downloader(urls[2],files[2]);
        downloader3.start();
        Downloader downloader4=new Downloader(urls[3],files[3]);
        downloader4.start();
    }

}

三、注:

本文借鉴了:

CSDN作者:石前有座桥的文章:week8多线程

链接:https://blog.csdn.net/lyw_321/article/details/79981323

并且加上自己的思考和修改完善。

猜你喜欢

转载自blog.csdn.net/qq_37768971/article/details/87863846