diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 82bd239..eb711d4 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -34,48 +34,35 @@ 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.printf("[Debug] Assigned Chunk #%d (%.1f MB) to Thread: %s%n", + chunk.getChunkId(), chunk.getChunkSizeMB(), workerThread.getName()); } // 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(); + System.out.println("\n[Main] Starting Progress Monitor..."); + monitorThread.start(); // 5. Start worker threads - // TODO: - // Start each worker thread in workerThreads. - // Use a loop and call start() on each thread. + System.out.println("[Main] Activating worker threads for parallel downloading...\n"); + for (Thread thread : workerThreads) { + thread.start(); + } // 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 { + System.out.println("[Main] Waiting for all download workers to complete..."); + 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, + System.out.println("[Main] Workers finished. Waiting for final monitor log..."); + monitorThread.join(); + } catch (InterruptedException e) { + System.out.println("Main thread was interrupted while waiting for workers: " + e.getMessage()); + } // 7. Print final report System.out.println();