通过Java下载文件时CPU使用率很高

时间:2013-12-01 00:17:35

标签: java download cpu-usage

我的一个游戏的发射器遇到了一个奇怪的问题。当程序在Linux(Ubuntu)上运行并且它开始下载LWJGL库时,CPU使用率会上升到操作系统在下载时几乎无法使用的程度。这似乎不会出现在Windows上(在同一台机器上测试)。

以下是负责下载文件的类(source):

import java.io.File;
import java.io.FileOutputStream;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;

public class Downloader implements Runnable {

    private URL url;
    private String fileName;
    private String name;

    /**
     * 
     * @param url The online URL of the file to download
     * @param fileName The local file to download into
     * @param name The name to be displayed in case of error
     */

    public Downloader(URL url, String fileName, String name){
        this.url = url;
        this.fileName = fileName;
        this.name = name;
    }

    public void run(){
        try {
            ReadableByteChannel rbc = Channels.newChannel(url.openStream());
            File file = new File(fileName);
            file.setReadable(true, false);
            file.setWritable(true, false);
            file.createNewFile();
            FileOutputStream os = new FileOutputStream(fileName);
            os.getChannel().transferFrom(rbc, 0, Launcher.getFileSize(url));
            os.close();
        }
        catch (Exception ex){
            ex.printStackTrace();
            Launcher.progress = "Failed to download " + name;
            Launcher.fail = "Errors occurred; see console for details";
            new Launcher().repaint();
        }
    }
}

有没有办法在没有命令行标志的情况下限制程序可用的资源?我明白,通常情况下,人们会睡觉,但我现在的实施情况是不可能的。

0 个答案:

没有答案