diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 82bd239..ba09981 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -34,36 +34,34 @@ 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("Assigned Chunk " + chunk.getChunkId()); } // 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(); // 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(); + + try { + for (Thread thread : workerThreads) { + thread.join(); + } + } catch (InterruptedException e) { + System.out.println("Interrupted."); + return; + } + + try { + monitorThread.join(); + } catch (InterruptedException e) { + System.out.println("Interrupted."); + return; + } - // 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(); - // } // TODO: // After all workers finish, the monitor thread may also need to stop.