Complete assignment #1

Merged
peyman merged 2 commits from develop into main 2026-07-18 17:10:20 +00:00
4 changed files with 47 additions and 64 deletions
Showing only changes of commit 277bdb91dd - Show all commits
+22 -11
View File
@@ -21,23 +21,34 @@ 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() + " started downloading...");
while (downloaded < chunkStatus.getChunkSizeMB()) {
// TODO: Generate a random sleep delay between min and max delay.
// TODO: Sleep for that delay.
// TODO: Generate a random download amount for this step.
// 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() + 1);
try {
Thread.sleep(delay);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
double downloadAmount = random.nextDouble(chunkStatus.getChunkSizeMB());
downloaded += downloadAmount;
if(downloaded > chunkStatus.getChunkSizeMB())
downloaded = chunkStatus.getChunkSizeMB();
chunkStatus.setDownloadedMB(downloaded);
System.out.printf("%s progress: %.2f / %.2f MB\n",
chunkStatus.getChunkId(), downloaded, chunkStatus.getChunkSizeMB());
}
// 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.setCompleted(true);
chunkStatus.setEndTimeMs(System.currentTimeMillis());
System.out.println(chunkStatus.getChunkId() + " finished downloading.");
}
}
+18 -31
View File
@@ -34,48 +34,35 @@ public class Main {
workerThreads.add(workerThread);
// TODO:
// Students may print helpful debug information here,
// for example which chunk is assigned to which worker thread.
System.out.printf("Chunk: %d, Thread: %s\n",
chunk.getChunkId(), workerThread.getName());
}
// 4. Create and start monitor thread
ProgressMonitor monitor = new ProgressMonitor(config, chunks);
Thread monitorThread = new Thread(monitor, "Progress-Monitor");
// TODO:
// 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 workerThread : workerThreads){
workerThread.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 workerThread : workerThreads){
try {
workerThread.join();
} catch(InterruptedException e){
throw new RuntimeException(e);
}
}
// 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,
try {
monitorThread.join();
} catch(InterruptedException e){
throw new RuntimeException(e);
}
// 7. Print final report
System.out.println();
+7 -13
View File
@@ -16,17 +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;
int completedChunks = 0;
@@ -55,8 +44,13 @@ 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. Downloaded file successfully.");
for(ChunkStatus chunk : chunks){
System.out.println(chunk);
}
break;
}
try {
Thread.sleep(monitorDelayMs);
-9
View File
@@ -1,9 +0,0 @@
public class temp {
public static void main(String[] args) {
Thread thread = new Thread(() -> {
System.out.println("Thread is running using a ...!");
});
thread.start();
}
}