complete all
This commit is contained in:
@@ -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)");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+19
-32
@@ -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) {
|
||||||
@@ -42,42 +43,28 @@ public class Main {
|
|||||||
// 4. Create and start monitor thread
|
// 4. Create and start monitor thread
|
||||||
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");
|
||||||
|
|
||||||
|
monitorThread.start();
|
||||||
|
|
||||||
// TODO:
|
for (Thread workerThread : workerThreads) {
|
||||||
// Start the monitor thread before starting the workers
|
workerThread.start();
|
||||||
// so that progress can be displayed while downloading happens.
|
}
|
||||||
//
|
|
||||||
// Example idea:
|
|
||||||
// monitorThread.start();
|
|
||||||
|
|
||||||
// 5. Start worker threads
|
for (Thread workerThread : workerThreads) {
|
||||||
// TODO:
|
try {
|
||||||
// Start each worker thread in workerThreads.
|
workerThread.join();
|
||||||
// Use a loop and call start() on each thread.
|
} catch (InterruptedException e) {
|
||||||
|
System.out.println("Thread was interrupted: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 6. Wait for workers to finish
|
try {
|
||||||
// TODO:
|
monitorThread.join();
|
||||||
// Wait for all worker threads to complete by calling 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:
|
|
||||||
// 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();
|
||||||
System.out.println("=== Final Report ===");
|
System.out.println("=== Final Report ===");
|
||||||
|
|
||||||
|
|||||||
@@ -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,14 +45,17 @@ 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);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
System.out.println("Progress monitor interrupted.");
|
System.out.println("Progress monitor interrupted.");
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user