week8多线程

6人阅读 评论(0) 收藏 举报
分类:

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

  1. import java.net.URL;
  2. import java.io.*;
  3.  
  4. class Downloader 
  5. {
  6.     public static void main(String[] args)
  7.         throws Exception
  8.     {
  9.         final URL[] urls = {
  10.             new URL("http://www.pku.edu.cn"),
  11.             new URL("http://www.baidu.com"),
  12.             new URL("http://www.sina.com.cn"),
  13.             new URL("http://www.dstang.com")
  14.         };
  15.         final String[] files = {
  16.             "pku.htm"
  17.             "baidu.htm",
  18.             "sina.htm"
  19.             "study.htm",
  20.         };
  21.  
  22.         for(int idx=0; idx<urls.length; idx++){
  23.             try{
  24.                 System.out.println( urls[idx] );
  25.                 download( urls[idx], files[idx]);
  26.             }catch(Exception ex){
  27.                 ex.printStackTrace();
  28.             }
  29.         }
  30.     }
  31.     static void download( URL url, String file)
  32.         throws IOException
  33.     {
  34.         try(InputStream input = url.openStream();
  35.             OutputStream output = new FileOutputStream(file))
  36.         {
  37.             byte[] data = new byte[1024];
  38.             int length;
  39.             while((length=input.read(data))!=-1){
  40.                 output.write(data,0,length);
  41.             }
  42.         }
  43.     }
  44. }


代码:

package week8;

import java.net.URL;
import java.io.*;
 
public class Downloader extends Thread{
	
	String file;
	URL url;
	
	public Downloader(URL url, String file){
		this.url=url;
		this.file=file;
	}
	

	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();
	    }

	}


public static void main(String[] args) throws Exception  {
		        
	final URL[] urls = {
            new URL("http://www.pku.edu.cn"),
            new URL("http://www.baidu.com"),
            new URL("http://www.sina.com.cn"),
            new URL("http://www.dstang.com")
        };
        final String[] files = {
            "pku.htm", 
            "baidu.htm",
            "sina.htm", 
            "study.htm",
        };
	
        	Thread t1=new Thread(new Downloader(urls[0],files[0]));
    		t1.start();
    		
    		Thread t2=new Thread(new Downloader(urls[1],files[1]));
    		t2.start();
    		
    		Thread t3=new Thread(new Downloader(urls[2],files[2]));
    		t3.start();
    		
    		Thread t4=new Thread(new Downloader(urls[3],files[3]));
    		t4.start();

	
	}
}
运行之后,在文件对应保存目录增加了4个htm文件


查看评论

Python-多线程

多线程多进程深度学习
  • 2017年08月21日 15:17

Ng 机器学习week8编程练习

主要是简单的聚类和pca降维 function idx = findClosestCentroids(X, centroids) K = size(centroids, 1); m=size(X,1...
  • a1310368974
  • a1310368974
  • 2016-11-29 13:31:19
  • 473

Coursera机器学习 week8 assignment

findClosestCentroids.m: K_temp = zeros(K, 1); for i = 1:size(idx, 1) %遍历所有样本 for j = 1:K %遍...
  • StephenFengz
  • StephenFengz
  • 2016-09-02 20:42:59
  • 1337

Coursera机器学习 Week8 笔记

K-means & PCA
  • u013515273
  • u013515273
  • 2017-08-23 14:24:24
  • 254

Stanford 机器学习笔记 Week8 Dimensionality Reduction

MotivationMotivation I: Data Compression降维可以做数据压缩,减少冗余信息从而减小存储空间。2D向1D降维: cm 和 inches都表示长度,属于冗余信息...
  • Baoli1008
  • Baoli1008
  • 2016-03-19 23:16:33
  • 581

Machine Learning(Andrew)Week8(下)

Dimensionality Reduction 这讲主要说数据降维。 数据降维就是减少特征个数,可以分为特征选择和特征提取两个步骤。原始数据采集的时候,数据的维度可能比较高,出现一些不重要特征,...
  • zndxABC
  • zndxABC
  • 2014-05-11 13:07:04
  • 583

20170129 Coursera Stanford-MachineLearning/Week8

Week8:Unsupervised Learning非监督学习Supervised learning & Unsupervised learning 监督学习与非监督学习Supervised lea...
  • SPARKKKK
  • SPARKKKK
  • 2017-01-29 18:06:14
  • 370

Stanford 机器学习笔记 Week8 Unsupervised Learning

ClusteringK-Means Algorithm一种经典的聚类算法,步骤很简单,分4步:1.首先随机选择K个聚类中心 2.对于Training Set中的每个点i,计算离i最近的中心c(i),...
  • Baoli1008
  • Baoli1008
  • 2016-03-19 21:12:03
  • 764

Machine Learning week 8 quiz: programming assignment-K-Means Clustering and PCA

一、ex7.m %% Machine Learning Online Class % Exercise 7 | Principle Component Analysis and K-Means C...
  • GarfieldEr007
  • GarfieldEr007
  • 2015-11-27 15:47:40
  • 4907

Ng机器学习 Week8 Unsupervised Learning

Clustering Principal Component Analysis
  • ZoeyyeoZ
  • ZoeyyeoZ
  • 2016-06-07 23:54:14
  • 808
    个人资料
    持之以恒
    等级:
    访问量: 2万+
    积分: 2771
    排名: 1万+
    最新评论