finishing DownloadWorker, ProgressMonitor, and Main
This commit is contained in:
@@ -22,22 +22,53 @@ 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;
|
||||||
|
System.out.printf("[%s] Started — Chunk #%d (%.1f MB)%n",
|
||||||
|
Thread.currentThread().getName(),
|
||||||
|
chunkStatus.getChunkId(),
|
||||||
|
chunkStatus.getChunkSizeMB());
|
||||||
|
|
||||||
// TODO: Print a message that this chunk has started downloading.
|
// TODO: Print a message that this chunk has started downloading.
|
||||||
|
|
||||||
while (downloaded < chunkStatus.getChunkSizeMB()) {
|
while (downloaded < chunkStatus.getChunkSizeMB()) {
|
||||||
|
int delay = config.getMinStepDelayMs()
|
||||||
|
+ random.nextInt(config.getMaxStepDelayMs() - config.getMinStepDelayMs() + 1);
|
||||||
|
|
||||||
// TODO: Generate a random sleep delay between min and max delay.
|
// TODO: Generate a random sleep delay between min and max delay.
|
||||||
// TODO: Sleep for that delay.
|
// TODO: Sleep for that delay.
|
||||||
// TODO: Generate a random download amount for this step.
|
// TODO: Generate a random download amount for this step.
|
||||||
// TODO: Increase downloaded, but do not go beyond chunk size.
|
// TODO: Increase downloaded, but do not go beyond chunk size.
|
||||||
// TODO: Save the updated downloaded value into chunkStatus.
|
// TODO: Save the updated downloaded value into chunkStatus.
|
||||||
// TODO: Optionally print step-by-step progress.
|
// TODO: Optionally print step-by-step progress.
|
||||||
}
|
try {
|
||||||
|
Thread.sleep(delay);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
double step = config.getMinStepDownloadMB()
|
||||||
|
+ random.nextDouble() * (config.getMaxStepDownloadMB() - config.getMinStepDownloadMB());
|
||||||
|
downloaded = Math.min(downloaded + step, chunkStatus.getChunkSizeMB());
|
||||||
|
chunkStatus.setDownloadedMB(downloaded);
|
||||||
|
System.out.printf("[%s] Chunk #%d → %.2f / %.1f MB (%.1f%%)%n",
|
||||||
|
Thread.currentThread().getName(),
|
||||||
|
chunkStatus.getChunkId(),
|
||||||
|
chunkStatus.getDownloadedMB(),
|
||||||
|
chunkStatus.getChunkSizeMB(),
|
||||||
|
chunkStatus.getProgressPercentage());
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Mark the chunk as completed.
|
// TODO: Mark the chunk as completed.
|
||||||
// TODO: Record the chunk end time in chunkStatus.
|
// TODO: Record the chunk end time in chunkStatus.
|
||||||
// TODO: Print a message that this chunk has finished downloading.
|
// TODO: Print a message that this chunk has finished downloading.
|
||||||
}
|
chunkStatus.setCompleted(true);
|
||||||
|
chunkStatus.setEndTimeMs(System.currentTimeMillis());
|
||||||
|
|
||||||
|
System.out.printf("[%s] Finished — Chunk #%d in %d ms%n",
|
||||||
|
Thread.currentThread().getName(),
|
||||||
|
chunkStatus.getChunkId(),
|
||||||
|
chunkStatus.getDownloadDurationMs());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+19
-1
@@ -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("Assigned " + chunk + " to " + workerThread.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
// 4. Create and start monitor thread
|
// 4. Create and start monitor thread
|
||||||
@@ -48,12 +49,15 @@ 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:
|
||||||
@@ -64,6 +68,20 @@ public class Main {
|
|||||||
// for (Thread thread : workerThreads) {
|
// for (Thread thread : workerThreads) {
|
||||||
// thread.join();
|
// thread.join();
|
||||||
// }
|
// }
|
||||||
|
// 6. Wait for workers to finish
|
||||||
|
try {
|
||||||
|
for (Thread thread : workerThreads) {
|
||||||
|
thread.join();
|
||||||
|
}
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
monitorThread.join();
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
}
|
||||||
|
|
||||||
// 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.
|
||||||
|
|||||||
@@ -57,7 +57,10 @@ 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("[Monitor] All chunks completed. Download finished!");
|
||||||
|
break;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
Thread.sleep(monitorDelayMs);
|
Thread.sleep(monitorDelayMs);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
|
|||||||
Reference in New Issue
Block a user