Develop #1
+18
-31
@@ -34,48 +34,35 @@ public class Main {
|
|||||||
|
|
||||||
workerThreads.add(workerThread);
|
workerThreads.add(workerThread);
|
||||||
|
|
||||||
// TODO:
|
System.out.printf("[Debug] Assigned Chunk #%d (%.1f MB) to Thread: %s%n",
|
||||||
// Students may print helpful debug information here,
|
chunk.getChunkId(), chunk.getChunkSizeMB(), workerThread.getName());
|
||||||
// for example which chunk is assigned to which worker thread.
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 4. Create and start monitor thread
|
// 4. Create and start monitor thread
|
||||||
ProgressMonitor monitor = new ProgressMonitor(config, chunks);
|
ProgressMonitor monitor = new ProgressMonitor(config, chunks);
|
||||||
Thread monitorThread = new Thread(monitor, "Progress-Monitor");
|
Thread monitorThread = new Thread(monitor, "Progress-Monitor");
|
||||||
|
|
||||||
// TODO:
|
System.out.println("\n[Main] Starting Progress Monitor...");
|
||||||
// Start the monitor thread before starting the workers
|
monitorThread.start();
|
||||||
// so that progress can be displayed while downloading happens.
|
|
||||||
//
|
|
||||||
// Example idea:
|
|
||||||
// monitorThread.start();
|
|
||||||
|
|
||||||
// 5. Start worker threads
|
// 5. Start worker threads
|
||||||
// TODO:
|
System.out.println("[Main] Activating worker threads for parallel downloading...\n");
|
||||||
// Start each worker thread in workerThreads.
|
for (Thread thread : workerThreads) {
|
||||||
// Use a loop and call start() on each thread.
|
thread.start();
|
||||||
|
}
|
||||||
|
|
||||||
// 6. Wait for workers to finish
|
// 6. Wait for workers to finish
|
||||||
// TODO:
|
try {
|
||||||
// Wait for all worker threads to complete by calling join().
|
System.out.println("[Main] Waiting for all download workers to complete...");
|
||||||
// This should be done inside a try-catch block for InterruptedException.
|
for (Thread thread : workerThreads) {
|
||||||
//
|
thread.join();
|
||||||
// Hint:
|
}
|
||||||
// for (Thread thread : workerThreads) {
|
|
||||||
// thread.join();
|
|
||||||
// }
|
|
||||||
|
|
||||||
// TODO:
|
System.out.println("[Main] Workers finished. Waiting for final monitor log...");
|
||||||
// After all workers finish, the monitor thread may also need to stop.
|
monitorThread.join();
|
||||||
// Depending on how ProgressMonitor is implemented, students may:
|
} catch (InterruptedException e) {
|
||||||
// - wait for it to finish on its own, or
|
System.out.println("Main thread was interrupted while waiting for workers: " + e.getMessage());
|
||||||
// - 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
|
// 7. Print final report
|
||||||
System.out.println();
|
System.out.println();
|
||||||
|
|||||||
Reference in New Issue
Block a user