idk
This commit is contained in:
@@ -21,23 +21,57 @@ public class DownloadWorker implements Runnable {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
// TODO: Record the chunk start time in chunkStatus.
|
||||
long startTime = System.currentTimeMillis();
|
||||
chunkStatus.setStartTime(startTime);
|
||||
|
||||
double downloaded = 0.0;
|
||||
|
||||
// TODO: Print a message that this chunk has started downloading.
|
||||
System.out.println("Chunk \"" + chunkStatus.getChunkId() + "\" started downloading.");
|
||||
|
||||
while (downloaded < chunkStatus.getChunkSizeMB()) {
|
||||
// TODO: Generate a random sleep delay between min and max delay.
|
||||
// TODO: Sleep for that delay.
|
||||
// TODO: Generate a random download amount for this step.
|
||||
// TODO: Increase downloaded, but do not go beyond chunk size.
|
||||
// TODO: Save the updated downloaded value into chunkStatus.
|
||||
// TODO: Optionally print step-by-step progress.
|
||||
|
||||
try {
|
||||
//Generate a random sleep delay between min and max delay.
|
||||
int minDelay = config.getMinDelayMs();
|
||||
int maxDelay = config.getMaxDelayMs();
|
||||
int randomDelay = random.nextInt((maxDelay - minDelay) + 1) + minDelay;
|
||||
|
||||
//Sleep for that delay.
|
||||
Thread.sleep(randomDelay);
|
||||
|
||||
//Generate a random download amount for this step.
|
||||
double minStep = config.getMinStepDelayMs();
|
||||
double maxStep = config.getMaxStepDelayMs();
|
||||
double randomStep = (maxStep + minStep)*random.nextDouble() + minStep;
|
||||
|
||||
//Increase downloaded, but do not go beyond chunk size.
|
||||
downloaded += randomStep;
|
||||
if (downloaded > chunkStatus.getChunkSizeMB()) {
|
||||
downloaded = chunkStatus.getChunkSizeMB();
|
||||
}
|
||||
|
||||
//Save the updated downloaded value into chunkStatus.
|
||||
chunkStatus.setDownloadedMB(downloaded);
|
||||
|
||||
//print step-by-step progress.
|
||||
System.out.printf("Chunk #%d: Downloaded %.2f / %.2f MB%n",
|
||||
chunkStatus.getChunkId(), downloaded, chunkStatus.getChunkSizeMB());
|
||||
|
||||
} catch (InterruptedException e) {
|
||||
System.out.println("Chunk \"" + chunkStatus.getChunkId() + "\" was interrupted!");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Mark the chunk as completed.
|
||||
// TODO: Record the chunk end time in chunkStatus.
|
||||
// TODO: Print a message that this chunk has finished downloading.
|
||||
//Mark the chunk as completed.
|
||||
chunkStatus.setCompleted(true);
|
||||
|
||||
//Record the chunk end time in chunkStatus.
|
||||
long endTime = System.currentTimeMillis();
|
||||
chunkStatus.setEndTimeMs(endTime);
|
||||
|
||||
//Print a message that this chunk has finished downloading.
|
||||
System.out.println("Chunk \"" + chunkStatus.getChunkId() + "\" has finished downloading.");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user