From 0adf5235e1987005afaa73e913a03e6094990db1 Mon Sep 17 00:00:00 2001 From: Arefe Date: Thu, 4 Jun 2026 15:30:02 +0330 Subject: [PATCH] complete practical part --- src/main/java/DownloadWorker.java | 44 +++++++++++++++++------- src/main/java/Main.java | 55 ++++++++++++------------------ src/main/java/ProgressMonitor.java | 17 +++------ 3 files changed, 59 insertions(+), 57 deletions(-) diff --git a/src/main/java/DownloadWorker.java b/src/main/java/DownloadWorker.java index 546de2d..559c839 100644 --- a/src/main/java/DownloadWorker.java +++ b/src/main/java/DownloadWorker.java @@ -1,3 +1,4 @@ +import java.sql.SQLOutput; import java.util.Random; /** @@ -21,23 +22,42 @@ 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.printf("worker-%d starting download of chunk (size: %.2f MB)%n", chunkStatus.getChunkId(), chunkStatus.getChunkSizeMB()); 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. + int delay = config.getMinStepDelayMs() + random.nextInt(config.getMaxStepDelayMs()-config.getMinStepDelayMs()+1); + try + { + Thread.sleep(delay); + } catch (InterruptedException e) + { + Thread.currentThread().interrupt(); + System.err.printf("worker-%d interrupted%n", chunkStatus.getChunkId()); + break; + } + + double stepSize = config.getMinStepDownloadMB() + random.nextDouble() * (config.getMaxStepDownloadMB() - config.getMinStepDownloadMB()); + downloaded += stepSize; + if (downloaded > chunkStatus.getChunkSizeMB()) + { + downloaded = chunkStatus.getChunkSizeMB(); + } + chunkStatus.setDownloadedMB(downloaded); + + System.out.printf("worker-%d progress: %.2f / %.2f MB (%.1f%%)%n", + chunkStatus.getChunkId(), + downloaded, + chunkStatus.getChunkSizeMB(), + (downloaded / chunkStatus.getChunkSizeMB()) * 100); } - // 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("worker-%d chunk download completed. Time: %d ms%n", + chunkStatus.getChunkId(), + chunkStatus.getDownloadDurationMs()); } } diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 82bd239..2926aa3 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -34,48 +34,37 @@ public class Main { workerThreads.add(workerThread); - // TODO: - // Students may print helpful debug information here, - // for example which chunk is assigned to which worker thread. + System.out.println("Created worker thread: " + workerThread.getName() + + " for chunk #" + chunk.getChunkId() + + " (size: " + chunk.getChunkSizeMB() + " MB)"); } // 4. Create and start monitor thread ProgressMonitor monitor = new ProgressMonitor(config, chunks); Thread monitorThread = new Thread(monitor, "Progress-Monitor"); - // TODO: - // Start the monitor thread before starting the workers - // so that progress can be displayed while downloading happens. - // - // Example idea: - // monitorThread.start(); + monitorThread.start(); + System.out.println("monitor thread started."); - // 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(); + System.out.println("started: "+thread.getName()); + } - // 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 + { + for (Thread thread : workerThreads) + { + thread.join(); - // 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, + } + } catch (InterruptedException e) + { + System.out.println("main thread interrupted while waiting for workers."); + Thread.currentThread().interrupt(); + return; + } // 7. Print final report System.out.println(); diff --git a/src/main/java/ProgressMonitor.java b/src/main/java/ProgressMonitor.java index 475c0e0..3490aa5 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,8 +45,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.printf("All chunks completed for %s. Monitor stopping>%n", fileName); + break; + } try { Thread.sleep(monitorDelayMs);