我的android程序中有两个线程同时启动,用于下载一些数据包。
ServerIP = WifiServer;
Thread thread22 = new Thread (new CallDownloader(fileToReceive, ServerIP, WifiServer, MobileServer));
thread22.start();
ServerIP = MobileServer;
Thread thread23 = new Thread (new CallDownloader(fileToReceive, ServerIP, WifiServer, MobileServer));
thread23.start();
我想同时从两台服务器下载一些文件。如果我在每个线程下使用 thread.joim();
ServerIP = WifiServer;
Thread thread22 = new Thread (new CallDownloader(fileToReceive, ServerIP, WifiServer, MobileServer));
thread22.start();
thread22.join();
ServerIP = MobileServer;
Thread thread23 = new Thread (new CallDownloader(fileToReceive, ServerIP, WifiServer, MobileServer));
thread23.start();
thread23.join();
代码不是并行的,而是序列化的。我不想要这个。我知道加入正在等待完成第一个,然后继续另一个。
当我取出 thread.join(); 它下载它想要的任何东西,而不是我想要下载的东西。并且仅使用第一个线程并且不会使用第二个线程。
我怎样才能避免加入?因为我需要线程来调用连接。如果您需要更多详细信息,请告诉我。
这是主要代码。
Queue<String> fileparts = new LinkedList<String>();
Time = SystemClock.currentThreadTimeMillis();
long Time2 = System.currentTimeMillis();
StartTime = Time2;
int intnumbOfChunks = Integer.parseInt(numbOfChunks);
intnumbOfChunks = 250;
for (int i = 0; i < intnumbOfChunks; i++) {
fileToClaim = "";
fileToClaim = "bigbang.mp4";
fileToClaim = String.format("%s.part%06d", fileToClaim, i);
fileparts.add(fileToClaim);
}
int i=0;
while (!fileparts.isEmpty()) {
fileToReceive = fileparts.peek();
ServerIP = WifiServer;
Thread thread22 = new Thread (new CallDownloader(fileToReceive, ServerIP, WifiServer, MobileServer));
thread22.start();
thread22.start();
try {
thread22.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ServerIP = MobileServer;
Thread thread23 = new Thread (new CallDownloader(fileToReceive, ServerIP, WifiServer, MobileServer));
thread23.start();
try {
thread22.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Thread thread3 = new Thread(new Join(fileToReceive));
thread3.start();
try {
thread3.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
fileparts.remove();}
下载线程是
public class Downloader {
final String fileToReceive;
boolean fileDownloaded = false;
Socket clientSocket;
public Downloader(String fileToReceive) {
this.fileToReceive = fileToReceive;
}
public void download(String ServerIP) {
//Socket clientSocket = null;
try {
System.out.println("kanw connect me ti porta 3248");
clientSocket = new Socket(ServerIP, 3248);
System.out.println("egine i sundesi me ti porta 3248");
System.out.println("stelnw to " + fileToReceive);
BufferedWriter BuffWriter = new BufferedWriter(
new OutputStreamWriter(clientSocket.getOutputStream()));
BuffWriter.write(fileToReceive + "\n");
BuffWriter.flush();
System.out.println("esteila to " + fileToReceive);
InputStream is = clientSocket.getInputStream();
File root = Environment.getExternalStorageDirectory();
System.out.println("dimiourgo to fakelo vid ");
File dir = new File(root.getAbsolutePath() + "/vid/");
// File dir = new File (root.getAbsolutePath());
dir.mkdirs();
System.out.println("arxizw tin lipsei tou chunk");
byte[] longBytes = new byte[8];
readFully(clientSocket, longBytes);
ByteBuffer bb = ByteBuffer.wrap(longBytes);
long fileLength = bb.getLong();
System.out.println("apothikeusi arxeiou");
// save
@SuppressWarnings("resource")
FileOutputStream f = new FileOutputStream(new File(dir,
fileToReceive));
byte[] buffer = new byte[1024];
int len1 = 0;
int readBytes = 0;
while ((len1 = is.read(buffer)) > 0) {
f.write(buffer, 0, len1);
readBytes += len1;
}
if (readBytes == fileLength) {
this.fileDownloaded = true;
}
System.out.println("Receiving the file " + fileToReceive);
System.out.println("File Recieved");
} catch (IOException ex) {
System.out.println("Client stopped");
ex.printStackTrace();
} finally {
try {
if (clientSocket != null && !clientSocket.isClosed()) {
clientSocket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void readFully(Socket clientSocket, byte[] buffer)
throws IOException {
System.out.println("readfully");
InputStream inputStream = clientSocket.getInputStream();
int remaining = buffer.length;
int read = 0;
while (remaining > 0) {
read = inputStream.read(buffer, buffer.length - remaining, remaining);
if (read < 0) {
throw new IOException();
}
remaining -= read;
}
System.out.println("exit");
}
public boolean IsDownloaded() {
return this.fileDownloaded;
}
}
CallDownloader 类是线程
public void run() {
System.out.println("connecting tou "+ServerIP);
Downloader d = new Downloader(fileToReceive);
//Downloader1 d1 = new Downloader1(fileToReceive);
d.download(ServerIP);
}