finishing DownloadWorker, ProgressMonitor, and Main

This commit is contained in:
2026-06-04 18:46:19 +03:30
parent 9e5c715088
commit 0240bdb2ea
3 changed files with 56 additions and 4 deletions
+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;
System.out.printf("[%s] Started — Chunk #%d (%.1f MB)%n",
Thread.currentThread().getName(),
chunkStatus.getChunkId(),
chunkStatus.getChunkSizeMB());
// TODO: Print a message that this chunk has started downloading.
while (downloaded < chunkStatus.getChunkSizeMB()) {
int delay = config.getMinStepDelayMs()
+ random.nextInt(config.getMaxStepDelayMs() - config.getMinStepDelayMs() + 1);
// 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(delay);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return;
}
double step = config.getMinStepDownloadMB()
+ random.nextDouble() * (config.getMaxStepDownloadMB() - config.getMinStepDownloadMB());
downloaded = Math.min(downloaded + step, chunkStatus.getChunkSizeMB());
chunkStatus.setDownloadedMB(downloaded);
System.out.printf("[%s] Chunk #%d → %.2f / %.1f MB (%.1f%%)%n",
Thread.currentThread().getName(),
chunkStatus.getChunkId(),
chunkStatus.getDownloadedMB(),
chunkStatus.getChunkSizeMB(),
chunkStatus.getProgressPercentage());
}
// 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.printf("[%s] Finished — Chunk #%d in %d ms%n",
Thread.currentThread().getName(),
chunkStatus.getChunkId(),
chunkStatus.getDownloadDurationMs());
}
}
+19 -1
View File
@@ -37,6 +37,7 @@ public class Main {
// TODO:
// Students may print helpful debug information here,
// for example which chunk is assigned to which worker thread.
System.out.println("Assigned " + chunk + " to " + workerThread.getName());
}
// 4. Create and start monitor thread
@@ -48,12 +49,15 @@ public class Main {
// so that progress can be displayed while downloading happens.
//
// Example idea:
// monitorThread.start();
monitorThread.start();
// 5. Start worker threads
// TODO:
// Start each worker thread in workerThreads.
// Use a loop and call start() on each thread.
for (Thread thread : workerThreads) {
thread.start();
}
// 6. Wait for workers to finish
// TODO:
@@ -64,6 +68,20 @@ public class Main {
// for (Thread thread : workerThreads) {
// thread.join();
// }
// 6. Wait for workers to finish
try {
for (Thread thread : workerThreads) {
thread.join();
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
try {
monitorThread.join();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
// TODO:
// After all workers finish, the monitor thread may also need to stop.
+4 -1
View File
@@ -57,7 +57,10 @@ public class ProgressMonitor implements Runnable {
// TODO:
// If all chunks are completed, print a final message and exit the loop
if (completedChunks == chunks.size()) {
System.out.println("[Monitor] All chunks completed. Download finished!");
break;
}
try {
Thread.sleep(monitorDelayMs);
} catch (InterruptedException e) {