This commit is contained in:
2026-06-27 15:34:07 +03:30
parent 9e5c715088
commit de653a85da
4 changed files with 69 additions and 75 deletions
+26 -20
View File
@@ -1,12 +1,5 @@
import java.util.Random;
/**
* Simulates downloading a single chunk of a file.
*
* <p>This class is intentionally provided as a skeleton for students.
* The main multithreading and simulation logic should be completed
* in the run() method.</p>
*/
public class DownloadWorker implements Runnable {
private final ChunkStatus chunkStatus;
@@ -21,23 +14,36 @@ public class DownloadWorker implements Runnable {
@Override
public void run() {
// TODO: Record the chunk start time in chunkStatus.
chunkStatus.setStartTimeMs(System.currentTimeMillis());
System.out.println(Thread.currentThread().getName() + " started downloading.");
double downloaded = 0.0;
double chunkSize = chunkStatus.getChunkSizeMB();
// TODO: Print a message that this chunk has started downloading.
while (downloaded < chunkSize) {
int delayMs = config.getMinStepDelayMs() + random.nextInt(config.getMaxStepDelayMs() - config.getMinStepDelayMs() + 1);
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 {
Thread.sleep(delayMs);
} catch (InterruptedException e) {
System.out.println(Thread.currentThread().getName() + " was interrupted.");
return;
}
double stepDownload = config.getMinStepDownloadMB() + (config.getMaxStepDownloadMB() - config.getMinStepDownloadMB()) * random.nextDouble();
downloaded += stepDownload;
if (downloaded > chunkSize) {
downloaded = chunkSize;
}
chunkStatus.setDownloadedMB(downloaded);
}
// TODO: Mark the chunk as completed.
// TODO: Record the chunk end time in chunkStatus.
// TODO: Print a message that this chunk has finished downloading.
chunkStatus.setCompleted(true);
chunkStatus.setEndTimeMs(System.currentTimeMillis());
System.out.println(Thread.currentThread().getName() + " finished downloading.");
}
}
}