⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 policethread.java

📁 用java编制的断点续传的源程序
💻 JAVA
字号:
package com.downfile;

import java.io.File;

/**
 * 监视线程,检测其它的线程是否已经运行完毕
 * 原理:
 *    在MultiThreadGetFile里定义一个全局静态boolean数组,在启动每个GetFileThread
 *    的时候,就将对应的数组的值设为false,当对应线程完成后就把对应的数组设为true,
 *    在当前线程采用不停检测是否所有数组的值都为true,如是那就说明所有的线程已经运行完
 *    毕,如果没有就继续检测。
 *    等到所有的GetFileThread线程都完成后,那么就调用文件拼合线程,合并下载的文件块并删除
 *    临时文件块。
 */
public class PoliceThread extends Thread {
    int totalThread;
    String localFileAddress;
    String localFileAddress_tmp;
    String urlFile;

    public PoliceThread(int totalThread, String localFileAddress, 
                        String localFileAddress_tmp, String urlFile) {
        this.totalThread = totalThread;
        this.localFileAddress = localFileAddress;
        this.localFileAddress_tmp = localFileAddress_tmp;
        this.urlFile = urlFile;
    }

    /**
     * 用去检测是否在临时目录下面是否还存在.tp的文件
     * @return 真为还有,即没有下载完成,假即下载完成
     */
    private boolean checkExistTPFile() {
        File file = new File(localFileAddress_tmp);
        String[] fileList = file.list();
        if (fileList.length > 0) {
            for (int i = 0; i < fileList.length; i++) {
                if (fileList[i].indexOf(".tp") > 0)
                    return true;
            }
        }
        else
            return true;
        return false;
    }

    public void run() {
        checkRunning();
        Thread thread = 
            new Thread(new FileCombination(localFileAddress, localFileAddress_tmp, 
                                           urlFile));
        thread.start();
    }

    private void checkRunning() {
        boolean isRun = true;
        while (isRun) {
            isRun = checkExistTPFile();
            if (isRun == false) {
                break;
            }
            try {
                this.sleep(2000);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -