From 30a4e3b734ce091f69386e10ffed8924efc0b647 Mon Sep 17 00:00:00 2001 From: SaraZoveydavian Date: Mon, 8 Jun 2026 13:36:28 +0330 Subject: [PATCH] completing all required classes --- src/main/java/DownloadWorker.java | 18 ++++++++++++++++++ src/main/java/Main.java | 21 ++++++++++++++++++--- src/main/java/ProgressMonitor.java | 5 +++++ 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/src/main/java/DownloadWorker.java b/src/main/java/DownloadWorker.java index 546de2d..12e65d6 100644 --- a/src/main/java/DownloadWorker.java +++ b/src/main/java/DownloadWorker.java @@ -1,5 +1,7 @@ import java.util.Random; +import static java.lang.Thread.sleep; + /** * Simulates downloading a single chunk of a file. * @@ -22,22 +24,38 @@ 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(chunkStatus.getChunkId() + " has started downloading..."); while (downloaded < chunkStatus.getChunkSizeMB()) { // TODO: Generate a random sleep delay between min and max delay. + int delayTime = random.nextInt(config.getMinStepDelayMs(), config.getMaxStepDelayMs()); // TODO: Sleep for that delay. + try { + Thread.sleep(delayTime); + } catch (InterruptedException e) { + throw new RuntimeException(e); // not sure about this one! + } // TODO: Generate a random download amount for this step. + double currentDownloaded = random.nextDouble(config.getMinStepDownloadMB(), config.getMaxStepDownloadMB()); // TODO: Increase downloaded, but do not go beyond chunk size. + while (downloaded <= 120) { + downloaded += currentDownloaded; + } // TODO: Save the updated downloaded value into chunkStatus. + chunkStatus.setDownloadedMB(downloaded); // TODO: Optionally print step-by-step progress. } // 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(chunkStatus.getChunkId() + " has finished downloading."); } } diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 82bd239..8fe7a5d 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -37,7 +37,9 @@ public class Main { // TODO: // Students may print helpful debug information here, // for example which chunk is assigned to which worker thread. + System.out.println(chunk.getChunkId() + " is assigned to " + workerThread.getName()); } + System.out.println(); // 4. Create and start monitor thread ProgressMonitor monitor = new ProgressMonitor(config, chunks); @@ -49,11 +51,15 @@ public class Main { // // 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 wT: workerThreads){ + wT.start(); + } // 6. Wait for workers to finish // TODO: @@ -61,9 +67,13 @@ public class Main { // This should be done inside a try-catch block for InterruptedException. // // Hint: - // for (Thread thread : workerThreads) { - // thread.join(); - // } + for (Thread thread : workerThreads) { + try { + thread.join(); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } // TODO: // After all workers finish, the monitor thread may also need to stop. @@ -72,6 +82,11 @@ public class Main { // - add a stopping mechanism in ProgressMonitor later. // // If your monitor finishes automatically, you may join it here. + try { + monitorThread.join(); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } // NOTE: // this final report may show 0 progress because no worker has actually run yet. diff --git a/src/main/java/ProgressMonitor.java b/src/main/java/ProgressMonitor.java index 475c0e0..eb1040f 100644 --- a/src/main/java/ProgressMonitor.java +++ b/src/main/java/ProgressMonitor.java @@ -57,6 +57,11 @@ 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("all chunks are completed!"); + break; + } try { Thread.sleep(monitorDelayMs);