This commit is contained in:
2026-06-04 19:52:17 +04:30
parent 9e5c715088
commit 57079f70f1
5 changed files with 99 additions and 2 deletions
+30
View File
@@ -22,22 +22,52 @@ 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("The chunk has started downloading.");
while (downloaded < chunkStatus.getChunkSizeMB()) {
// TODO: Generate a random sleep delay between min and max delay.
int max1 = config.getMaxStepDelayMs();
int min1 = config.getMinStepDelayMs();
int randomDelay = random.nextInt(max1 - min1 + 1) + min1;
// TODO: Sleep for that delay.
try {
Thread.sleep(randomDelay);
} catch (InterruptedException e) {
//...
}
// TODO: Generate a random download amount for this step.
double max2 = config.getMaxStepDownloadMB();
double min2 = config.getMinStepDownloadMB();
double randomDownloadedAmount = random.nextDouble(max2 - min2 + 1) + min2;
// TODO: Increase downloaded, but do not go beyond chunk size.
downloaded += randomDownloadedAmount;
// TODO: Save the updated downloaded value into chunkStatus.
chunkStatus.setDownloadedMB(downloaded);
// TODO: Optionally print step-by-step progress.
System.out.println(String.format("Chunk-%d: %.1f/%.1f MB",
chunkStatus.getChunkId(),
chunkStatus.getDownloadedMB(),
chunkStatus.getChunkSizeMB()));
}
// 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(String.format("Chunk-%d has finished downloading.", chunkStatus.getChunkId()));
}
}
+17
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("Chunk " + chunk.getChunkId() + " is assigned to " + workerThread.getName());
}
// 4. Create and start monitor thread
@@ -49,11 +50,15 @@ public class Main {
//
// 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:
@@ -64,6 +69,13 @@ public class Main {
// for (Thread thread : workerThreads) {
// thread.join();
// }
for (Thread thread : workerThreads) {
try {
thread.join();
} catch (InterruptedException e) {
//...
}
}
// TODO:
// After all workers finish, the monitor thread may also need to stop.
@@ -72,6 +84,11 @@ public class Main {
// - add a stopping mechanism in ProgressMonitor later.
//
// If your monitor finishes automatically, you may join it here.
try {
monitorThread.join();
} catch (InterruptedException e) {
//...
}
// NOTE:
// this final report may show 0 progress because no worker has actually run yet.
+4
View File
@@ -57,6 +57,10 @@ 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("All chunks are completed.");
return;
}
try {
Thread.sleep(monitorDelayMs);
+2 -2
View File
@@ -1,6 +1,6 @@
fileName=movie.mkv
totalSizeMB=120
chunkCount=6
totalSizeMB=170
chunkCount=5
minStepDelayMs=80
maxStepDelayMs=200
minStepDownloadMB=2