3 Commits
Author SHA1 Message Date
Matin 10a49e83e5 finish 2026-06-13 20:28:01 +04:30
Matin 3f0342ef88 Implement ProgressMonitor 2026-06-13 19:57:41 +04:30
Matin bcf960805e Implement download worker 2026-06-12 20:18:28 +04:30
8 changed files with 49 additions and 4 deletions
@@ -22,22 +22,45 @@ public class DownloadWorker implements Runnable {
@Override
public void run() {
// TODO: Record the chunk start time in chunkStatus.
chunkStatus.setStartTimeMs(System.currentTimeMilliS()) ;
double downloaded = 0.0;
// 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()) {
// 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.
Thread.sleep(delay) ;
// 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.
downloaded += stepDownload ;
if (downloaded > chunkStatus.getChunkSizeMB())
{
downloaded = chunkStatus.getChunkSizeMB() ;
}
// TODO: Save the updated downloaded value into chunkStatus.
chunkStatus.setDownloadedMB(downloaded) ;
// TODO: Optionally print step-by-step progress.
}
// TODO: Mark the chunk as completed.
chunkStatus.setCompleted(true );
// TODO: Record the chunk end time in chunkStatus.
chunkStatus.setEndTimeMs(System.currentTimeMillis()) ;
// 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:
// Students may print helpful debug information here,
// 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
@@ -48,12 +49,17 @@ public class Main {
// 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() ;
}
// 6. Wait for workers to finish
// TODO:
@@ -61,9 +67,13 @@ public class Main {
// This should be done inside a try-catch block for InterruptedException.
//
// Hint:
// for (Thread thread : workerThreads) {
// thread.join();
// }
try {
for (Thread thread : workerThreads) {
thread.join();
}
} catch (java.lang.Exception e) {
System.out.println("Main thread was interrupted while waiting for workers.")
}
// TODO:
// After all workers finish, the monitor thread may also need to stop.
@@ -76,6 +86,11 @@ public class Main {
// NOTE:
// this final report may show 0 progress because no worker has actually run yet.
// 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
System.out.println();
@@ -27,6 +27,7 @@ public class ProgressMonitor implements Runnable {
// 6. Otherwise sleep for monitorDelayMs and continue
while (true) {
double totalDownloadedMB = 0.0;
int completedChunks = 0;
@@ -57,6 +58,12 @@ public class ProgressMonitor implements Runnable {
// TODO:
// 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 {
Thread.sleep(monitorDelayMs);