JOKER1072752180 2016-03-23 10:55 采纳率: 0%
浏览 3301

跪求哪位大神讲解一下main方法中new Thread中传入的lambda是什么?

我是个新手,刚刚在树上看到了这个程序,DownUtil中有private的内部类继承了thread类,对于main方法中的thread()怎么调用的不太懂,求耐心看看,大神能够解答一下;

import java.io.*;
import java.net.*;

public class DownUtil {
private String path;
private String targetFile;
private int threadNum;
private DownThread[] threads;
private int fileSize;

public DownUtil(String path, String targetFile, int threadNum) {
    this.path = path;
    this.threadNum = threadNum;
    this.targetFile = targetFile;
    threads = new DownThread[threadNum];
}

public void download() throws Exception {
    URL url = new URL(path);
    HttpURLConnection conn = (HttpURLConnection)url.openConnection();
    conn.setConnectTimeout(5 * 1000);
    conn.setRequestMethod("GET");
    conn.setRequestProperty("Accept", "image/gif, image/jpeg, image/pjpeg, image/pjpeg," + 
    "application/x-shockwave-flash, application/xaml+xml," + "application/vnd.ms-xpsdocument, application/x-ms-xbap," + 
    "application/x-ms-application, application/vnd.ms-excel," + "application/vnd.ms-powerpoint, application/msword, */*");

    conn.setRequestProperty("Aceept-Language", "zh-CN");
    conn.setRequestProperty("Charset", "UTF-8");
    conn.setRequestProperty("Connection", "Keep-Alive");

    fileSize = conn.getContentLength();
    conn.disconnect();
    int currentPartSize = fileSize / threadNum + 1;
    RandomAccessFile file = new RandomAccessFile(targetFile, "rw");

    file.setLength(fileSize);
    file.close();
    for(int i=0; i<threadNum; i++) {
        int startPos = i * currentPartSize;
        RandomAccessFile currentPart = new RandomAccessFile(targetFile, "rw");
        currentPart.seek(startPos);
        threads[i] = new DownThread(startPos, currentPartSize, currentPart);
        threads[i].start();
    }
}

public double getCompleteRate() {
    int sumSize = 0;
    for(int i=0; i<threadNum; i++) {
        sumSize += threads[i].length;
    }
    return sumSize * 1.0 / fileSize;
}

private class DownThread extends Thread {
    private int startPos;
    private int currentPartSize;
    private RandomAccessFile currentPart;

    public int length;
    public DownThread(int startPos, int currentPartSize, RandomAccessFile currentPart) {
        this.startPos = startPos;
        this.currentPartSize = currentPartSize;
        this.currentPart = currentPart;
    }

    public void run() {
        try {
            URL url = new URL(path);
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
            conn.setConnectTimeout(5 * 1000);
            conn.setRequestMethod("GET");
            conn.setRequestProperty("Accept", "image/gif, image/jpeg, image/pjpeg, image/pjpeg," + 
            "application/x-shockwave-flash, application/xaml+xml," + "application/vnd.ms-xpsdocument, application/x-ms-xbap," + 
            "application/x-ms-application, application/vnd.ms-excel," + "application/vnd.ms-powerpoint, application/msword, */*");
            conn.setRequestProperty("Accept-Language", "zh-CN");
            conn.setRequestProperty("Charset", "UTF-8");
            InputStream inStream = conn.getInputStream();
            inStream.skip(this.startPos);
            byte[] buffer = new byte[1024];
            int hasRead = 0;
            while(length < currentPartSize && (hasRead = inStream.read(buffer)) != -1) {
                currentPart.write(buffer, 0, hasRead);
                length += hasRead;
            }
            currentPart.close();
            inStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }   
}

}

public class MultiThreadDown {
public static void mian(String[] args) throws Exception {
final DownUtil downUtil = new DownUtil("http://www.wmpic.me/61188/1458367711_GpOvzHwr.jpg", "ios.png", 4);
downUtil.download();

    new Thread(() -> {
        while(downUtil.getCompleteRate() < 1) {
            System.out.println("已完成:" + downUtil.getConpleteRate());
            try {
                Thread.sleep(1000);
            } catch(Exception e) {}
            }
        }).start();
}   

}

  • 写回答

1条回答

  • 给我一台时光机 2016-03-23 11:17
    关注

    这是Java8中的Lambda表达式,相当于实现了一个Runneable接口的内部类,通过Thread的start来调用这个内部类.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?