complete all

This commit is contained in:
2026-06-03 10:13:53 +03:30
parent 4ed8ce49d7
commit be90a69b4b
4 changed files with 33 additions and 52 deletions
+3 -2
View File
@@ -32,13 +32,14 @@ public class DownloadWorker implements Runnable {
double down = random.nextDouble();
downloaded = Math.min(downloaded + down, chunkStatus.getChunkSizeMB());
chunkStatus.setDownloadedMB(chunkStatus.getDownloadedMB() + downloaded);
chunkStatus.setDownloadedMB(chunkStatus.getDownloadedMB() + down);
}
chunkStatus.setCompleted(true);
Long endTime = System.currentTimeMillis();
chunkStatus.setEndTimeMs(endTime);
System.out.println("Chunk: " + chunkStatus.getChunkId() + " has completed downloading.");
System.out.println("Chunk: " + chunkStatus.getChunkId() +
" has completed downloading (" + chunkStatus.getDownloadDurationMs() +" ms)");
}
}
+19 -32
View File
@@ -1,5 +1,6 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class Main {
public static void main(String[] args) {
@@ -42,42 +43,28 @@ public class Main {
// 4. Create and start monitor thread
ProgressMonitor monitor = new ProgressMonitor(config, chunks);
Thread monitorThread = new Thread(monitor, "Progress-Monitor");
monitorThread.start();
// TODO:
// Start the monitor thread before starting the workers
// so that progress can be displayed while downloading happens.
//
// Example idea:
// monitorThread.start();
for (Thread workerThread : workerThreads) {
workerThread.start();
}
// 5. Start worker threads
// TODO:
// Start each worker thread in workerThreads.
// Use a loop and call start() on each thread.
for (Thread workerThread : workerThreads) {
try {
workerThread.join();
} catch (InterruptedException e) {
System.out.println("Thread was interrupted: " + e.getMessage());
}
}
// 6. Wait for workers to finish
// TODO:
// Wait for all worker threads to complete by calling join().
// This should be done inside a try-catch block for InterruptedException.
//
// Hint:
// for (Thread thread : workerThreads) {
// thread.join();
// }
try {
monitorThread.join();
}
catch (InterruptedException e) {
System.out.println("Thread was interrupted: " + e.getMessage());
}
// TODO:
// After all workers finish, the monitor thread may also need to stop.
// Depending on how ProgressMonitor is implemented, students may:
// - wait for it to finish on its own, or
// - add a stopping mechanism in ProgressMonitor later.
//
// If your monitor finishes automatically, you may join it here.
// NOTE:
// this final report may show 0 progress because no worker has actually run yet.
// Until students complete the thread start/join TODOs above,
// 7. Print final report
System.out.println();
System.out.println("=== Final Report ===");
+10 -17
View File
@@ -16,16 +16,6 @@ public class ProgressMonitor implements Runnable {
@Override
public void run() {
// TODO:
// Repeatedly check chunk progress until all chunks are completed.
// In each loop:
// 1. Read the downloaded size from every chunk
// 2. Add all downloaded amounts to totalDownloadedMB
// 3. Count completed chunks
// 4. Print a progress message
// 5. If completedChunks == chunks.size(), print a final monitor message and stop
// 6. Otherwise sleep for monitorDelayMs and continue
while (true) {
double totalDownloadedMB = 0.0;
@@ -55,14 +45,17 @@ 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("download completed!");
break;
} else {
try {
Thread.sleep(monitorDelayMs);
} catch (InterruptedException e) {
System.out.println("Progress monitor interrupted.");
return;
try {
Thread.sleep(monitorDelayMs);
} catch (InterruptedException e) {
System.out.println("Progress monitor interrupted.");
return;
}
}
}
}
+1 -1
View File
@@ -1,6 +1,6 @@
fileName=movie.mkv
totalSizeMB=120
chunkCount=6
chunkCount=10
minStepDelayMs=80
maxStepDelayMs=200
minStepDownloadMB=2