develop
This commit is contained in:
@@ -23,8 +23,11 @@ public class DownloadWorker implements Runnable {
|
||||
public void run() {
|
||||
// TODO: Record the chunk start time in chunkStatus.
|
||||
double downloaded = 0.0;
|
||||
chunkStatus.setStartTimeMs(System.currentTimeMillis());
|
||||
chunkStatus.setDownloadedMB(downloaded);
|
||||
|
||||
// TODO: Print a message that this chunk has started downloading.
|
||||
System.out.println("Chunk #" + chunkStatus.getChunkId() + " started downloading.");
|
||||
|
||||
while (downloaded < chunkStatus.getChunkSizeMB()) {
|
||||
// TODO: Generate a random sleep delay between min and max delay.
|
||||
@@ -33,11 +36,28 @@ public class DownloadWorker implements Runnable {
|
||||
// TODO: Increase downloaded, but do not go beyond chunk size.
|
||||
// TODO: Save the updated downloaded value into chunkStatus.
|
||||
// TODO: Optionally print step-by-step progress.
|
||||
int delay = config.getMinStepDelayMs() + random.nextInt(config.getMaxStepDelayMs() - config.getMinStepDelayMs());
|
||||
try {
|
||||
Thread.sleep(delay);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
downloaded+=0.5;
|
||||
|
||||
if (downloaded > chunkStatus.getChunkSizeMB())
|
||||
downloaded = chunkStatus.getChunkSizeMB();
|
||||
chunkStatus.setDownloadedMB(downloaded);
|
||||
|
||||
}
|
||||
|
||||
|
||||
// TODO: Mark the chunk as completed.
|
||||
// TODO: Record the chunk end time in chunkStatus.
|
||||
// TODO: Print a message that this chunk has finished downloading.
|
||||
chunkStatus.setEndTimeMs(System.currentTimeMillis());
|
||||
chunkStatus.setCompleted(true);
|
||||
System.out.println("Chunk #" + chunkStatus.getChunkId() + " finished downloading.");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+11
-7
@@ -47,23 +47,27 @@ public class Main {
|
||||
// Start the monitor thread before starting the workers
|
||||
// so that progress can be displayed while downloading happens.
|
||||
//
|
||||
// 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 t:workerThreads){
|
||||
t.start();
|
||||
}
|
||||
|
||||
// 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();
|
||||
// }
|
||||
for (Thread t : workerThreads) {
|
||||
try {
|
||||
t.join();
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
// TODO:
|
||||
// After all workers finish, the monitor thread may also need to stop.
|
||||
|
||||
@@ -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("Procsses Done!");
|
||||
break;
|
||||
}
|
||||
|
||||
try {
|
||||
Thread.sleep(monitorDelayMs);
|
||||
|
||||
Reference in New Issue
Block a user