Compare commits
6
Commits
main
...
ef6a05b445
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ef6a05b445 | ||
|
|
fa47ea93ae | ||
|
|
2eae5431f9 | ||
|
|
ba67e5b73c | ||
|
|
7fe4d5079d | ||
|
|
85955e9c61 |
@@ -0,0 +1,69 @@
|
||||
## Question 1:
|
||||
|
||||
Output
|
||||
```bash
|
||||
Calling run()
|
||||
Running in: main
|
||||
Calling start()
|
||||
Running in: Thread-2
|
||||
```
|
||||
|
||||
When we call `t1.run()`, we are just calling a method in the current thread(the main thread).
|
||||
No extra thread is created.
|
||||
|
||||
But if we call `t2.start()`, the code runs in a separate thread and use multithreading.
|
||||
|
||||
## Question 2:
|
||||
|
||||
Output:
|
||||
```bash
|
||||
Main thread ends.
|
||||
Daemon thread running...
|
||||
```
|
||||
|
||||
- The main thread finishes immediately and the other thread is stopped right after because it's a daemon thread.
|
||||
|
||||
|
||||
- If we remove `thread.setDaemon(true)`, the thread won't be a daemon anymore. So when the main thread finishes, the other thread will continue and the output would be :
|
||||
|
||||
```bash
|
||||
Main thread ends.
|
||||
Daemon thread running...
|
||||
Daemon thread running...
|
||||
Daemon thread running...
|
||||
Daemon thread running...
|
||||
Daemon thread running...
|
||||
Daemon thread running...
|
||||
Daemon thread running...
|
||||
Daemon thread running...
|
||||
Daemon thread running...
|
||||
Daemon thread running...
|
||||
Daemon thread running...
|
||||
Daemon thread running...
|
||||
Daemon thread running...
|
||||
Daemon thread running...
|
||||
Daemon thread running...
|
||||
Daemon thread running...
|
||||
Daemon thread running...
|
||||
Daemon thread running...
|
||||
Daemon thread running...
|
||||
Daemon thread running...
|
||||
```
|
||||
|
||||
- Background Tasks: Tasks that shouldn’t block the application from shutting down.
|
||||
Examples:
|
||||
Garbage Collection: Memory management.
|
||||
Auto-save: Periodically saving temporary drafts.
|
||||
Monitoring/Heartbeats: Checking system health or connection status in the background.
|
||||
|
||||
|
||||
## Question 3:
|
||||
|
||||
Output:
|
||||
```bash
|
||||
Thread is running using a ...!
|
||||
```
|
||||
|
||||
- **Syntax**: This is a Lambda Expression, a shortcut in to write code blocks (functions) concisely.
|
||||
- **Difference**: Your code is just a shorter version of the standard `implements Runnable` approach.
|
||||
You don’t need to create a separate class anymore; you just pass the logic directly as a lambda. It’s cleaner and less boilerplate code.
|
||||
@@ -21,23 +21,55 @@ public class DownloadWorker implements Runnable {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
// TODO: Record the chunk start time in chunkStatus.
|
||||
// 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.
|
||||
// Print a message that this chunk has started downloading.
|
||||
System.out.println("Starting download for Chunk " + chunkStatus.getChunkId());
|
||||
|
||||
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.
|
||||
|
||||
// Generate a random sleep delay between min and max delay.
|
||||
long minDelay = config.getMinStepDelayMs();
|
||||
long maxDelay = config.getMaxStepDelayMs();
|
||||
long sleepTime = minDelay + (long) ((maxDelay - minDelay) * random.nextDouble());
|
||||
|
||||
// Sleep for that delay.
|
||||
try {
|
||||
Thread.sleep(sleepTime);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
// Generate a random download amount for this step.
|
||||
double minStep = config.getMinStepDownloadMB();
|
||||
double maxStep = config.getMaxStepDownloadMB();
|
||||
double downloadAmount = minStep + ((maxStep - minStep) * random.nextDouble());
|
||||
|
||||
// Increase downloaded, but do not go beyond chunk size.
|
||||
if(downloaded + downloadAmount >= chunkStatus.getChunkSizeMB())
|
||||
downloaded = chunkStatus.getChunkSizeMB();
|
||||
else
|
||||
downloaded += downloadAmount;
|
||||
|
||||
// Save the updated downloaded value into chunkStatus.
|
||||
chunkStatus.setDownloadedMB(downloaded);
|
||||
|
||||
// Optionally print step-by-step progress.
|
||||
System.out.print("Chunk " + chunkStatus.getChunkId() + " | downloaded: ");
|
||||
System.out.printf("%.2f%n", 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.
|
||||
// Mark the chunk as completed.
|
||||
chunkStatus.setCompleted(true);
|
||||
|
||||
// Record the chunk end time in chunkStatus.
|
||||
chunkStatus.setEndTimeMs(System.currentTimeMillis());
|
||||
|
||||
// Print a message that this chunk has finished downloading.
|
||||
System.out.println("\nChunk " + chunkStatus.getChunkId() + " | COMPLETED!\n");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+20
-14
@@ -34,45 +34,51 @@ 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.println("Assigned Chunk " + chunk.getChunkId()
|
||||
+ " (" + chunk.getChunkSizeMB() + " MB) to " + workerThread.getName());
|
||||
}
|
||||
System.out.println();
|
||||
|
||||
// 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 thread : workerThreads) {
|
||||
thread.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();
|
||||
// }
|
||||
|
||||
// TODO:
|
||||
// This should be done inside a try-catch block for InterruptedException
|
||||
for (Thread thread : workerThreads) {
|
||||
try {
|
||||
thread.join();
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
// 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.
|
||||
|
||||
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.
|
||||
// Until students complete the thread start/join TODOs above,
|
||||
|
||||
@@ -16,7 +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
|
||||
@@ -45,7 +44,7 @@ public class ProgressMonitor implements Runnable {
|
||||
}
|
||||
|
||||
System.out.printf(
|
||||
"Progress for %s: %.1f/%.1f MB (%.2f%%), completed chunks: %d/%d%n",
|
||||
"\nProgress for %s: %.1f/%.1f MB (%.2f%%), completed chunks: %d/%d%n%n",
|
||||
fileName,
|
||||
totalDownloadedMB,
|
||||
(double) totalSizeMB,
|
||||
@@ -54,9 +53,11 @@ public class ProgressMonitor implements Runnable {
|
||||
chunks.size()
|
||||
);
|
||||
|
||||
|
||||
// TODO:
|
||||
// If all chunks are completed, print a final message and exit the loop
|
||||
if(completedChunks == chunks.size()){
|
||||
System.out.println(fileName + " downloaded completely.");
|
||||
break;
|
||||
}
|
||||
|
||||
try {
|
||||
Thread.sleep(monitorDelayMs);
|
||||
|
||||
Reference in New Issue
Block a user