From 0240bdb2eadcc205a9f8aa44947a8bea828e9421 Mon Sep 17 00:00:00 2001 From: ava Date: Thu, 4 Jun 2026 18:46:19 +0330 Subject: [PATCH] finishing DownloadWorker, ProgressMonitor, and Main --- src/main/java/DownloadWorker.java | 35 ++++++++++++++++++++++++++++-- src/main/java/Main.java | 20 ++++++++++++++++- src/main/java/ProgressMonitor.java | 5 ++++- 3 files changed, 56 insertions(+), 4 deletions(-) diff --git a/src/main/java/DownloadWorker.java b/src/main/java/DownloadWorker.java index 546de2d..70acffe 100644 --- a/src/main/java/DownloadWorker.java +++ b/src/main/java/DownloadWorker.java @@ -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()); +} } diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 82bd239..2eda18f 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -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. diff --git a/src/main/java/ProgressMonitor.java b/src/main/java/ProgressMonitor.java index 475c0e0..4bf0e7d 100644 --- a/src/main/java/ProgressMonitor.java +++ b/src/main/java/ProgressMonitor.java @@ -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) {