Develop #1

Open
HadiSharifi wants to merge 4 commits from develop into main
4 changed files with 33 additions and 52 deletions
Showing only changes of commit be90a69b4b - Show all commits
+3 -2
View File
@@ -32,13 +32,14 @@ public class DownloadWorker implements Runnable {
double down = random.nextDouble(); double down = random.nextDouble();
downloaded = Math.min(downloaded + down, chunkStatus.getChunkSizeMB()); downloaded = Math.min(downloaded + down, chunkStatus.getChunkSizeMB());
chunkStatus.setDownloadedMB(chunkStatus.getDownloadedMB() + downloaded); chunkStatus.setDownloadedMB(chunkStatus.getDownloadedMB() + down);
} }
chunkStatus.setCompleted(true); chunkStatus.setCompleted(true);
Long endTime = System.currentTimeMillis(); Long endTime = System.currentTimeMillis();
chunkStatus.setEndTimeMs(endTime); chunkStatus.setEndTimeMs(endTime);
System.out.println("Chunk: " + chunkStatus.getChunkId() + " has completed downloading."); System.out.println("Chunk: " + chunkStatus.getChunkId() +
" has completed downloading (" + chunkStatus.getDownloadDurationMs() +" ms)");
} }
} }
+18 -31
View File
@@ -1,5 +1,6 @@
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Random;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
@@ -43,41 +44,27 @@ public class Main {
ProgressMonitor monitor = new ProgressMonitor(config, chunks); ProgressMonitor monitor = new ProgressMonitor(config, chunks);
Thread monitorThread = new Thread(monitor, "Progress-Monitor"); Thread monitorThread = new Thread(monitor, "Progress-Monitor");
// TODO: monitorThread.start();
// Start the monitor thread before starting the workers
// so that progress can be displayed while downloading happens.
//
// Example idea:
// monitorThread.start();
// 5. Start worker threads for (Thread workerThread : workerThreads) {
// TODO: workerThread.start();
// Start each worker thread in workerThreads. }
// Use a loop and call start() on each thread.
// 6. Wait for workers to finish for (Thread workerThread : workerThreads) {
// TODO: try {
// Wait for all worker threads to complete by calling join(). workerThread.join();
// This should be done inside a try-catch block for InterruptedException. } catch (InterruptedException e) {
// System.out.println("Thread was interrupted: " + e.getMessage());
// Hint: }
// for (Thread thread : workerThreads) { }
// thread.join();
// }
// TODO: try {
// After all workers finish, the monitor thread may also need to stop. monitorThread.join();
// Depending on how ProgressMonitor is implemented, students may: }
// - wait for it to finish on its own, or catch (InterruptedException e) {
// - add a stopping mechanism in ProgressMonitor later. System.out.println("Thread was interrupted: " + e.getMessage());
// }
// 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();
System.out.println("=== Final Report ==="); System.out.println("=== Final Report ===");
+5 -12
View File
@@ -16,16 +16,6 @@ public class ProgressMonitor implements Runnable {
@Override @Override
public void run() { 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) { while (true) {
double totalDownloadedMB = 0.0; double totalDownloadedMB = 0.0;
@@ -55,8 +45,10 @@ public class ProgressMonitor implements Runnable {
); );
// TODO: if (completedChunks == chunks.size()) {
// If all chunks are completed, print a final message and exit the loop System.out.println("download completed!");
break;
} else {
try { try {
Thread.sleep(monitorDelayMs); Thread.sleep(monitorDelayMs);
@@ -66,4 +58,5 @@ public class ProgressMonitor implements Runnable {
} }
} }
} }
}
} }
+1 -1
View File
@@ -1,6 +1,6 @@
fileName=movie.mkv fileName=movie.mkv
totalSizeMB=120 totalSizeMB=120
chunkCount=6 chunkCount=10
minStepDelayMs=80 minStepDelayMs=80
maxStepDelayMs=200 maxStepDelayMs=200
minStepDownloadMB=2 minStepDownloadMB=2