From be90a69b4b6385870259810683d406bbc6488915 Mon Sep 17 00:00:00 2001 From: HadiSharifi Date: Wed, 3 Jun 2026 10:13:53 +0330 Subject: [PATCH] complete all --- src/main/java/DownloadWorker.java | 5 ++- src/main/java/Main.java | 51 ++++++++++---------------- src/main/java/ProgressMonitor.java | 27 +++++--------- src/main/resources/download_config.txt | 2 +- 4 files changed, 33 insertions(+), 52 deletions(-) diff --git a/src/main/java/DownloadWorker.java b/src/main/java/DownloadWorker.java index 573f354..4e1cc60 100644 --- a/src/main/java/DownloadWorker.java +++ b/src/main/java/DownloadWorker.java @@ -32,13 +32,14 @@ public class DownloadWorker implements Runnable { double down = random.nextDouble(); downloaded = Math.min(downloaded + down, chunkStatus.getChunkSizeMB()); - chunkStatus.setDownloadedMB(chunkStatus.getDownloadedMB() + downloaded); + chunkStatus.setDownloadedMB(chunkStatus.getDownloadedMB() + down); } chunkStatus.setCompleted(true); Long endTime = System.currentTimeMillis(); chunkStatus.setEndTimeMs(endTime); - System.out.println("Chunk: " + chunkStatus.getChunkId() + " has completed downloading."); + System.out.println("Chunk: " + chunkStatus.getChunkId() + + " has completed downloading (" + chunkStatus.getDownloadDurationMs() +" ms)"); } } diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 82bd239..5109729 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,5 +1,6 @@ import java.util.ArrayList; import java.util.List; +import java.util.Random; public class Main { public static void main(String[] args) { @@ -42,42 +43,28 @@ public class Main { // 4. Create and start monitor thread ProgressMonitor monitor = new ProgressMonitor(config, chunks); Thread monitorThread = new Thread(monitor, "Progress-Monitor"); + + monitorThread.start(); - // TODO: - // Start the monitor thread before starting the workers - // so that progress can be displayed while downloading happens. - // - // Example idea: - // monitorThread.start(); + for (Thread workerThread : workerThreads) { + workerThread.start(); + } - // 5. Start worker threads - // TODO: - // Start each worker thread in workerThreads. - // Use a loop and call start() on each thread. + for (Thread workerThread : workerThreads) { + try { + workerThread.join(); + } catch (InterruptedException e) { + System.out.println("Thread was interrupted: " + e.getMessage()); + } + } - // 6. Wait for workers to finish - // TODO: - // Wait for all worker threads to complete by calling join(). - // This should be done inside a try-catch block for InterruptedException. - // - // Hint: - // for (Thread thread : workerThreads) { - // thread.join(); - // } + try { + monitorThread.join(); + } + catch (InterruptedException e) { + System.out.println("Thread was interrupted: " + e.getMessage()); + } - // TODO: - // After all workers finish, the monitor thread may also need to stop. - // Depending on how ProgressMonitor is implemented, students may: - // - wait for it to finish on its own, or - // - add a stopping mechanism in ProgressMonitor later. - // - // If your monitor finishes automatically, you may join it here. - - // NOTE: - // this final report may show 0 progress because no worker has actually run yet. - // Until students complete the thread start/join TODOs above, - - // 7. Print final report System.out.println(); System.out.println("=== Final Report ==="); diff --git a/src/main/java/ProgressMonitor.java b/src/main/java/ProgressMonitor.java index 475c0e0..b9b982d 100644 --- a/src/main/java/ProgressMonitor.java +++ b/src/main/java/ProgressMonitor.java @@ -16,16 +16,6 @@ public class ProgressMonitor implements Runnable { @Override public void run() { - // TODO: - // Repeatedly check chunk progress until all chunks are completed. - // In each loop: - // 1. Read the downloaded size from every chunk - // 2. Add all downloaded amounts to totalDownloadedMB - // 3. Count completed chunks - // 4. Print a progress message - // 5. If completedChunks == chunks.size(), print a final monitor message and stop - // 6. Otherwise sleep for monitorDelayMs and continue - while (true) { double totalDownloadedMB = 0.0; @@ -55,14 +45,17 @@ 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("download completed!"); + break; + } else { - try { - Thread.sleep(monitorDelayMs); - } catch (InterruptedException e) { - System.out.println("Progress monitor interrupted."); - return; + try { + Thread.sleep(monitorDelayMs); + } catch (InterruptedException e) { + System.out.println("Progress monitor interrupted."); + return; + } } } } diff --git a/src/main/resources/download_config.txt b/src/main/resources/download_config.txt index 6ddd6ca..f0ff5f2 100644 --- a/src/main/resources/download_config.txt +++ b/src/main/resources/download_config.txt @@ -1,6 +1,6 @@ fileName=movie.mkv totalSizeMB=120 -chunkCount=6 +chunkCount=10 minStepDelayMs=80 maxStepDelayMs=200 minStepDownloadMB=2