implement DownloadWorker.java
This commit is contained in:
@@ -21,23 +21,36 @@ public class DownloadWorker implements Runnable {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
// TODO: Record the chunk start time in chunkStatus.
|
chunkStatus.setStartTimeMs(System.currentTimeMillis());
|
||||||
double downloaded = 0.0;
|
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()) {
|
while (downloaded < chunkStatus.getChunkSizeMB()) {
|
||||||
// TODO: Generate a random sleep delay between min and max delay.
|
try {
|
||||||
// TODO: Sleep for that delay.
|
int delay = config.getMinStepDelayMs()
|
||||||
// TODO: Generate a random download amount for this step.
|
+ random.nextInt(config.getMaxStepDelayMs() - config.getMinStepDelayMs() + 1);
|
||||||
// TODO: Increase downloaded, but do not go beyond chunk size.
|
Thread.sleep(delay);
|
||||||
// TODO: Save the updated downloaded value into chunkStatus.
|
|
||||||
// TODO: Optionally print step-by-step progress.
|
double stepDownload = config.getMinStepDownloadMB()
|
||||||
|
+ random.nextDouble() * (config.getMaxStepDownloadMB() - config.getMinStepDownloadMB());
|
||||||
|
|
||||||
|
downloaded += stepDownload;
|
||||||
|
if (downloaded > chunkStatus.getChunkSizeMB()) {
|
||||||
|
downloaded = chunkStatus.getChunkSizeMB();
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Mark the chunk as completed.
|
chunkStatus.setDownloadedMB(downloaded);
|
||||||
// TODO: Record the chunk end time in chunkStatus.
|
|
||||||
// TODO: Print a message that this chunk has finished downloading.
|
} 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