Develop #1

Open
Sarazpr wants to merge 2 commits from develop into main
3 changed files with 41 additions and 3 deletions
Showing only changes of commit 30a4e3b734 - Show all commits
+18
View File
@@ -1,5 +1,7 @@
import java.util.Random;
import static java.lang.Thread.sleep;
/**
* Simulates downloading a single chunk of a file.
*
@@ -22,22 +24,38 @@ 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(chunkStatus.getChunkId() + " has started downloading...");
while (downloaded < chunkStatus.getChunkSizeMB()) {
// TODO: Generate a random sleep delay between min and max delay.
int delayTime = random.nextInt(config.getMinStepDelayMs(), config.getMaxStepDelayMs());
// TODO: Sleep for that delay.
try {
Thread.sleep(delayTime);
} catch (InterruptedException e) {
throw new RuntimeException(e); // not sure about this one!
}
// TODO: Generate a random download amount for this step.
double currentDownloaded = random.nextDouble(config.getMinStepDownloadMB(), config.getMaxStepDownloadMB());
// TODO: Increase downloaded, but do not go beyond chunk size.
while (downloaded <= 120) {
downloaded += currentDownloaded;
}
// TODO: Save the updated downloaded value into chunkStatus.
chunkStatus.setDownloadedMB(downloaded);
// TODO: Optionally print step-by-step progress.
}
// 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(chunkStatus.getChunkId() + " has finished downloading.");
}
}
+18 -3
View File
@@ -37,7 +37,9 @@ 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.getChunkId() + " is assigned to " + workerThread.getName());
}
System.out.println();
// 4. Create and start monitor thread
ProgressMonitor monitor = new ProgressMonitor(config, chunks);
@@ -49,11 +51,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 wT: workerThreads){
wT.start();
}
// 6. Wait for workers to finish
// TODO:
@@ -61,9 +67,13 @@ public class Main {
// This should be done inside a try-catch block for InterruptedException.
//
// Hint:
// for (Thread thread : workerThreads) {
// thread.join();
// }
for (Thread thread : workerThreads) {
try {
thread.join();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
// TODO:
// After all workers finish, the monitor thread may also need to stop.
@@ -72,6 +82,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) {
throw new RuntimeException(e);
}
// NOTE:
// this final report may show 0 progress because no worker has actually run yet.
+5
View File
@@ -57,6 +57,11 @@ 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!");
break;
}
try {
Thread.sleep(monitorDelayMs);