Develop #1
@@ -21,23 +21,36 @@ public class DownloadWorker implements Runnable {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
// TODO: Record the chunk start time in chunkStatus.
|
||||
chunkStatus.setStartTimeMs(System.currentTimeMillis());
|
||||
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 {
|
||||
int delay = config.getMinStepDelayMs()
|
||||
+ random.nextInt(config.getMaxStepDelayMs() - config.getMinStepDelayMs() + 1);
|
||||
Thread.sleep(delay);
|
||||
|
||||
// TODO: Mark the chunk as completed.
|
||||
// TODO: Record the chunk end time in chunkStatus.
|
||||
// TODO: Print a message that this chunk has finished downloading.
|
||||
double stepDownload = config.getMinStepDownloadMB()
|
||||
+ random.nextDouble() * (config.getMaxStepDownloadMB() - config.getMinStepDownloadMB());
|
||||
|
||||
downloaded += stepDownload;
|
||||
if (downloaded > chunkStatus.getChunkSizeMB()) {
|
||||
downloaded = chunkStatus.getChunkSizeMB();
|
||||
}
|
||||
|
||||
chunkStatus.setDownloadedMB(downloaded);
|
||||
|
||||
} catch (InterruptedException e) {
|
||||
System.err.println("[Worker-" + chunkStatus.getChunkId() + "] Download interrupted.");
|
||||
Thread.currentThread().interrupt();
|
||||
return;
|
||||
}
|
||||
}
|
||||
chunkStatus.setCompleted(true);
|
||||
chunkStatus.setEndTimeMs(System.currentTimeMillis());
|
||||
System.out.println("[Worker-" + chunkStatus.getChunkId() + "] Download completed.");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user