implement Main.java
This commit is contained in:
+20
-14
@@ -34,45 +34,51 @@ public class Main {
|
|||||||
|
|
||||||
workerThreads.add(workerThread);
|
workerThreads.add(workerThread);
|
||||||
|
|
||||||
// TODO:
|
|
||||||
// Students may print helpful debug information here,
|
// Students may print helpful debug information here,
|
||||||
// for example which chunk is assigned to which worker thread.
|
// for example which chunk is assigned to which worker thread.
|
||||||
|
System.out.println("Assigned Chunk " + chunk.getChunkId()
|
||||||
|
+ " (" + chunk.getChunkSizeMB() + " MB) to " + workerThread.getName());
|
||||||
}
|
}
|
||||||
|
System.out.println();
|
||||||
|
|
||||||
// 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:
|
|
||||||
// Start the monitor thread before starting the workers
|
// Start the monitor thread before starting the workers
|
||||||
// so that progress can be displayed while downloading happens.
|
// so that progress can be displayed while downloading happens.
|
||||||
//
|
//
|
||||||
// Example idea:
|
// Example idea:
|
||||||
// monitorThread.start();
|
monitorThread.start();
|
||||||
|
|
||||||
// 5. Start worker threads
|
// 5. Start worker threads
|
||||||
// TODO:
|
|
||||||
// Start each worker thread in workerThreads.
|
// Start each worker thread in workerThreads.
|
||||||
// Use a loop and call start() on each thread.
|
// Use a loop and call start() on each thread.
|
||||||
|
for(Thread thread : workerThreads) {
|
||||||
|
thread.start();
|
||||||
|
}
|
||||||
|
|
||||||
// 6. Wait for workers to finish
|
// 6. Wait for workers to finish
|
||||||
// TODO:
|
|
||||||
// Wait for all worker threads to complete by calling join().
|
// Wait for all worker threads to complete by calling join().
|
||||||
// This should be done inside a try-catch block for InterruptedException.
|
// This should be done inside a try-catch block for InterruptedException
|
||||||
//
|
for (Thread thread : workerThreads) {
|
||||||
// Hint:
|
try {
|
||||||
// for (Thread thread : workerThreads) {
|
thread.join();
|
||||||
// thread.join();
|
} catch (InterruptedException e) {
|
||||||
// }
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
// TODO:
|
}
|
||||||
// After all workers finish, the monitor thread may also need to stop.
|
// After all workers finish, the monitor thread may also need to stop.
|
||||||
// Depending on how ProgressMonitor is implemented, students may:
|
// Depending on how ProgressMonitor is implemented, students may:
|
||||||
// - wait for it to finish on its own, or
|
// - wait for it to finish on its own, or
|
||||||
// - add a stopping mechanism in ProgressMonitor later.
|
// - add a stopping mechanism in ProgressMonitor later.
|
||||||
//
|
//
|
||||||
// If your monitor finishes automatically, you may join it here.
|
// If your monitor finishes automatically, you may join it here.
|
||||||
|
try {
|
||||||
|
monitorThread.join();
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
// NOTE:
|
// NOTE:
|
||||||
// this final report may show 0 progress because no worker has actually run yet.
|
// this final report may show 0 progress because no worker has actually run yet.
|
||||||
// Until students complete the thread start/join TODOs above,
|
// Until students complete the thread start/join TODOs above,
|
||||||
|
|||||||
Reference in New Issue
Block a user