implement DownloadWorker.java

This commit is contained in:
2026-06-08 17:04:13 +03:30
parent 7fe4d5079d
commit ba67e5b73c
+31
View File
@@ -22,22 +22,53 @@ 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("Starting download for Chunk ID: " + chunkStatus.getChunkId());
while (downloaded < chunkStatus.getChunkSizeMB()) {
// TODO: Generate a random sleep delay between min and max delay.
long minDelay = config.getMinStepDelayMs();
long maxDelay = config.getMaxStepDelayMs();
long sleepTime = minDelay + (long) ((maxDelay - minDelay) * random.nextDouble());
// TODO: Sleep for that delay.
try {
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
// TODO: Generate a random download amount for this step.
double minStep = config.getMinStepDownloadMB();
double maxStep = config.getMaxStepDownloadMB();
double downloadAmount = minStep + ((maxStep - minStep) * random.nextDouble());
// TODO: Increase downloaded, but do not go beyond chunk size.
if(downloaded + downloadAmount >= chunkStatus.getChunkSizeMB())
downloaded = chunkStatus.getChunkSizeMB();
else
downloaded += downloadAmount;
// TODO: Save the updated downloaded value into chunkStatus.
chunkStatus.setDownloadedMB(downloaded);
// TODO: Optionally print step-by-step progress.
System.out.println("Chunk ID: " + chunkStatus.getChunkId() + " | downloaded: " + downloaded);
}
// TODO: Mark the chunk as completed.
chunkStatus.setCompleted(true);
// TODO: Record the chunk end time in chunkStatus.
chunkStatus.setEndTimeMs(System.currentTimeMillis());
// TODO: Print a message that this chunk has finished downloading.
System.out.println("Chunk ID: " + chunkStatus.getChunkId() + " | COMPLETED!");
}
}