我正在java中编写一个像IDM这样的小应用程序。 但这有很多例外。 这是实现runnable的Downloader类的代码,我想用它来进行多线程处理。
public class Downloader implements Runnable{
private DataInputStream inputStream;
private byte[][] fileData;
private int index;
private int size;
public Downloader(DataInputStream inputStream, byte[][] fileData, int index, int size) {
this.inputStream = inputStream;
this.fileData = fileData;
this.index = index;
this.size = size;
}
public synchronized void run() {
try{
inputStream.skipBytes(index * size);
for(int i= 0;i<size;i++){
fileData[index][i] = inputStream.readByte();
System.out.println("It works : " + index);
}
}
catch(Exception e){
System.out.println(e.getMessage());
}
}}
这是我的主要课程
public class Main {
public static void main(String[] args) {
String s;
//Scanner input = new Scanner(System.in);
//System.out.print("Enter file destination : ");
//s = input.nextLine();
s = "http://video.varzesh3.com/video/clip1/92/uclip/fun/gaf_6_borhani.mp4";
URL url;
URLConnection connection;
DataInputStream inputStream;
FileOutputStream outStream;
byte[][] fileData;
try{
url = new URL(s);
connection = url.openConnection();
inputStream = new DataInputStream(connection.getInputStream());
fileData = new byte[8][connection.getContentLength() / 4];
int size = connection.getContentLength() / 4;
Runnable d0 = new Downloader(inputStream, fileData, 0, size);
Runnable d1 = new Downloader(inputStream, fileData, 1, size);
Runnable d2 = new Downloader(inputStream, fileData, 2, size);
Runnable d3 = new Downloader(inputStream, fileData, 3, size);
Thread thread0 = new Thread(d0);
Thread thread1 = new Thread(d1);
Thread thread2 = new Thread(d2);
Thread thread3 = new Thread(d3);
thread0.start();
thread1.start();
thread2.start();
thread3.start();
inputStream.close();
String path = "C:\\Users\\NetTest\\Desktop\\test.mp4";
outStream = new FileOutputStream(new File(path));
outStream.write(fileData[0]);
/*outStream.write(fileData[1]);
outStream.write(fileData[2]);
outStream.write(fileData[3]);
outStream.write(fileData[4]);
outStream.write(fileData[5]);
outStream.write(fileData[6]);
outStream.write(fileData[7]);*/
outStream.close();
}
catch(Exception e){
System.out.println(e.getMessage());
}
}}
但是当我运行它时会发生这种情况
It works: 0
null
null
null
null
我现在该怎么办?
答案 0 :(得分:1)
您的代码中有2个问题。
在当前状态下,您close()
InputStream
所有Thread
在您启动它们后直接尝试读取它们( - &gt;在运行时)。要解决此问题,您可以调用join()
类的Thread
方法。在您的情况下,您必须为所有4 Threads
调用它以确保它们已完成。
如果我理解正确,您希望将下载File
分成4个部分同时下载。
要执行此操作,您需要4个独立的InputStreams
。 (目前您正在使用ONE [另请参阅:Java Object Copying])
所以要改变它,你的代码看起来像这样:
public class Downloader implements Runnable{
private byte[][] fileData;
private int index;
private int size;
private URL url;
public Downloader(URL url, byte[][] fileData, int index, int size) {
this.fileData = fileData;
this.index = index;
this.size = size;
this.url = url;
}
public synchronized void run() {
try{
URLConnection connection = url.openConnection();
DataInputStream inputStream = new DataInputStream(connection.getInputStream());
inputStream.skipBytes(index * size);
for(int i= 0;i<size;i++){
fileData[index][i] = inputStream.readByte();
System.out.println("It works : " + index);
}
}catch(Exception e){
System.out.println(e.getMessage());
}
}
}