DownloadWorker completed.

This commit is contained in:
2026-06-11 23:23:01 +03:30
parent bcb381f84b
commit b2bf918030
3 changed files with 55 additions and 11 deletions
+4
View File
@@ -105,4 +105,8 @@ public class ChunkStatus {
completed ? " [Completed]" : "" completed ? " [Completed]" : ""
); );
} }
public void setStartTime(long startTime) {
}
} }
+6
View File
@@ -67,4 +67,10 @@ public class DownloadConfig {
", maxStepDownloadMB=" + maxStepDownloadMB + ", maxStepDownloadMB=" + maxStepDownloadMB +
'}'; '}';
} }
public int getMinDelayMs() {
}
public int getMaxDelayMs() {
}
} }
+45 -11
View File
@@ -21,23 +21,57 @@ public class DownloadWorker implements Runnable {
@Override @Override
public void run() { public void run() {
// TODO: Record the chunk start time in chunkStatus. long startTime = System.currentTimeMillis();
chunkStatus.setStartTime(startTime);
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.
// TODO: Sleep for that delay. try {
// TODO: Generate a random download amount for this step. //Generate a random sleep delay between min and max delay.
// TODO: Increase downloaded, but do not go beyond chunk size. int minDelay = config.getMinDelayMs();
// TODO: Save the updated downloaded value into chunkStatus. int maxDelay = config.getMaxDelayMs();
// TODO: Optionally print step-by-step progress. 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. //Mark the chunk as completed.
// TODO: Record the chunk end time in chunkStatus. chunkStatus.setCompleted(true);
// TODO: Print a message that this chunk has finished downloading.
//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.");
} }
} }