Merge pull request 'Develop' (#1) from develop into main

Reviewed-on: #1
This commit was merged in pull request #1.
This commit is contained in:
2026-08-01 00:48:53 +00:00
8 changed files with 49 additions and 4 deletions
@@ -22,22 +22,45 @@ public class DownloadWorker implements Runnable {
@Override @Override
public void run() { public void run() {
// TODO: Record the chunk start time in chunkStatus. // TODO: Record the chunk start time in chunkStatus.
chunkStatus.setStartTimeMs(System.currentTimeMilliS()) ;
double downloaded = 0.0; double downloaded = 0.0;
// TODO: Print a message that this chunk has started downloading. // TODO: Print a message that this chunk has started downloading.
System.out.println(Thread.currentThread().getName() + " started downloading Chunk #" + chunkStatus.getChunkId());
while (downloaded < chunkStatus.getChunkSizeMB()) { while (downloaded < chunkStatus.getChunkSizeMB()) {
// TODO: Generate a random sleep delay between min and max delay. // TODO: Generate a random sleep delay between min and max delay.
int minDelay = config.getMinStepDelayMs() ;
int maxDelay = config.getMaxStepDalayMs() ;
int delay = minDelay + random.nextInt(maxDelay-minDelay +1) ;
// TODO: Sleep for that delay. // TODO: Sleep for that delay.
Thread.sleep(delay) ;
// TODO: Generate a random download amount for this step. // TODO: Generate a random download amount for this step.
double minStep = config.getMinStepDownloadMB() ;
double maxStep = config.getMaxStepDownloadMB() ;
double stepDownload = minStep + ((maxStep-minStep)*random.nextDouble()) ;
// TODO: Increase downloaded, but do not go beyond chunk size. // TODO: Increase downloaded, but do not go beyond chunk size.
downloaded += stepDownload ;
if (downloaded > chunkStatus.getChunkSizeMB())
{
downloaded = chunkStatus.getChunkSizeMB() ;
}
// TODO: Save the updated downloaded value into chunkStatus. // TODO: Save the updated downloaded value into chunkStatus.
chunkStatus.setDownloadedMB(downloaded) ;
// TODO: Optionally print step-by-step progress. // TODO: Optionally print step-by-step progress.
} }
// TODO: Mark the chunk as completed. // TODO: Mark the chunk as completed.
chunkStatus.setCompleted(true );
// TODO: Record the chunk end time in chunkStatus. // TODO: Record the chunk end time in chunkStatus.
chunkStatus.setEndTimeMs(System.currentTimeMillis()) ;
// TODO: Print a message that this chunk has finished downloading. // TODO: Print a message that this chunk has finished downloading.
System.out.println(Thread.currentThread().getName() + " finished downloading Chunk #" + chunkStatus.getChunkId());
} }
} }
+19 -4
View File
@@ -37,6 +37,7 @@ public class Main {
// TODO: // 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("Assignment Chunk : " + chunk.chunk.getChunkId() + "(" + chunk.getChunkSizeMB) + "MB)" + workerThread.getName()) ;
} }
// 4. Create and start monitor thread // 4. Create and start monitor thread
@@ -48,12 +49,17 @@ public class Main {
// 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: // 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: // TODO:
@@ -61,9 +67,13 @@ public class Main {
// This should be done inside a try-catch block for InterruptedException. // This should be done inside a try-catch block for InterruptedException.
// //
// Hint: // Hint:
// for (Thread thread : workerThreads) { try {
// thread.join(); for (Thread thread : workerThreads) {
// } thread.join();
}
} catch (java.lang.Exception e) {
System.out.println("Main thread was interrupted while waiting for workers.")
}
// TODO: // 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.
@@ -76,6 +86,11 @@ public class Main {
// 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,
try {
monitorThread.join();
} catch (InterruptedException e) {
System.out.println("Main thread was interrupted while waiting for monitor.");
}
// 7. Print final report // 7. Print final report
System.out.println(); System.out.println();
@@ -27,6 +27,7 @@ public class ProgressMonitor implements Runnable {
// 6. Otherwise sleep for monitorDelayMs and continue // 6. Otherwise sleep for monitorDelayMs and continue
while (true) { while (true) {
double totalDownloadedMB = 0.0; double totalDownloadedMB = 0.0;
int completedChunks = 0; int completedChunks = 0;
@@ -57,6 +58,12 @@ public class ProgressMonitor implements Runnable {
// TODO: // TODO:
// If all chunks are completed, print a final message and exit the loop // If all chunks are completed, print a final message and exit the loop
if (completedChunks == chunks.size())
{
System.out.println("Progress Monitor: All chunks successfully downloaded. Exiting monitor thread.");
break;
}
try { try {
Thread.sleep(monitorDelayMs); Thread.sleep(monitorDelayMs);