diff --git a/Report.md b/Report.md new file mode 100644 index 0000000..6a25b07 --- /dev/null +++ b/Report.md @@ -0,0 +1,92 @@ + +## Question 1: `start()` vs `run()` + +### What output do you get from the program? + + + +The first call (`t1.run()`) prints `Running in: main` because it executes on the **main thread** directly — no new thread is created. The second call (`t2.start()`) properly spawns a new thread, so it prints `Running in: Thread-2`. + +The `Thread.sleep(100)` between the two calls ensures `t2`'s output isn't interleaved with the first, but the ordering of the second line is still technically non-deterministic (it will almost always appear after `"Calling start()"`). + +### What's the difference between `start()` and `run()`? + +| | `run()` | `start()` | +|---|---|---| +| **Execution** | Runs on the **calling thread** (like a normal method call) | Spawns a **new OS thread** and runs `run()` on it | +| **Concurrency** | None — sequential, blocking | Concurrent — the calling thread continues immediately | +| **Thread name** | Uses the calling thread's name (`main`) | Uses the new thread's name (`Thread-2`) | +| **Thread lifecycle** | Does not transition the thread to `RUNNABLE` state | Properly starts the thread lifecycle | + +Calling `run()` directly is just a regular method call. Only `start()` actually creates a new thread. + +--- + +## Question 2: Daemon Threads + +### What output do you get from the program? + +``` +Main thread ends. +Daemon thread running... +Daemon thread running... +(possibly a few more lines, then the program exits abruptly) +``` + +The exact number of "Daemon thread running..." lines is non-deterministic — typically 0 to 2. The JVM shuts down as soon as the **main thread** finishes, which kills all remaining daemon threads immediately, regardless of what they're doing. + +### What happens if you remove `thread.setDaemon(true)`? + +The thread becomes a regular (non-daemon) **user thread**. The JVM will **not exit** until all user threads complete. In this case, the loop runs all 20 iterations (taking ~10 seconds), printing `"Daemon thread running..."` 20 times before the program ends. + +### Real-life use cases of daemon threads + +- **Garbage Collector** – The JVM's own GC runs as a daemon thread; it should never prevent the JVM from shutting down. +- **Background logging** – Flushing logs or metrics to a file/server periodically, where losing the last few entries on shutdown is acceptable. +- **Heartbeat / keep-alive threads** – Sending periodic pings to a server while the application is alive. +- **Cache invalidation** – A background thread that evicts stale entries from an in-memory cache. +- **Auto-save** – Periodically saving a draft in an editor; the user closing the app shouldn't be blocked by this. + +--- + +## Question 3: A Shorter Way to Create Threads (Lambda) + +### What output do you get from the program? + +``` +Thread is running using a ...! +``` + +(Printed from the newly spawned thread.) + +### What is the `() -> { ... }` syntax called? + +It is called a **lambda expression** (introduced in Java 8). It provides a concise way to implement a **functional interface** — an interface with exactly one abstract method. Since `Runnable` has only one method (`run()`), a lambda can be used anywhere a `Runnable` is expected. + +### How is this different from extending `Thread` or implementing `Runnable`? + +| Approach | Code required | Reusability | Notes | +|---|---|---|---| +| `extends Thread` | Full class definition | Low — must subclass `Thread`, can't extend anything else | Tightly couples logic to thread machinery | +| `implements Runnable` | Full class + `new Thread(r)` | Higher — decouples task from thread | Preferred for named/reusable task classes | +| **Lambda** | One-liner inline | Low — anonymous, inline only | Best for short, one-off tasks | + +The lambda approach is essentially **anonymous shorthand for `implements Runnable`** — the compiler generates an implementation of `Runnable.run()` under the hood. It is the most concise option but is best suited for simple, short tasks. For complex or reusable tasks, a named class implementing `Runnable` is clearer. + +```java +// These three are functionally equivalent: + +// 1. Class extending Thread +class MyThread extends Thread { + public void run() { System.out.println("Running"); } +} +new MyThread().start(); + +// 2. Anonymous Runnable +new Thread(new Runnable() { + public void run() { System.out.println("Running"); } +}).start(); + +// 3. Lambda (shortest) +new Thread(() -> System.out.println("Running")).start(); +``` diff --git a/src/main/java/DownloadWorker.java b/src/main/java/DownloadWorker.java index 546de2d..4e1cc60 100644 --- a/src/main/java/DownloadWorker.java +++ b/src/main/java/DownloadWorker.java @@ -1,12 +1,5 @@ import java.util.Random; -/** - * Simulates downloading a single chunk of a file. - * - *
This class is intentionally provided as a skeleton for students. - * The main multithreading and simulation logic should be completed - * in the run() method.
- */ public class DownloadWorker implements Runnable { private final ChunkStatus chunkStatus; @@ -21,23 +14,32 @@ public class DownloadWorker implements Runnable { @Override public void run() { - // TODO: Record the chunk start time in chunkStatus. + double downloaded = 0.0; - - // TODO: Print a message that this chunk has started downloading. - + long startTime = System.currentTimeMillis(); + chunkStatus.setStartTimeMs(startTime); + System.out.println("Chunk: " + chunkStatus.getChunkId() + " has 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. + + long randomSleep = random.nextLong(config.getMinStepDelayMs(), config.getMaxStepDelayMs()); + + try { + Thread.sleep(randomSleep); + } + catch (InterruptedException e) { + System.out.println(e.getMessage()); + } + + double down = random.nextDouble(); + downloaded = Math.min(downloaded + down, chunkStatus.getChunkSizeMB()); + chunkStatus.setDownloadedMB(chunkStatus.getDownloadedMB() + down); } - // 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); + Long endTime = System.currentTimeMillis(); + chunkStatus.setEndTimeMs(endTime); + System.out.println("Chunk: " + chunkStatus.getChunkId() + + " has completed downloading (" + chunkStatus.getDownloadDurationMs() +" ms)"); } } diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 82bd239..b69bede 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,5 +1,6 @@ import java.util.ArrayList; import java.util.List; +import java.util.Random; public class Main { public static void main(String[] args) { @@ -19,13 +20,13 @@ public class Main { System.out.println("Chunk count: " + config.getChunkCount()); System.out.println(); - // 2. Create chunks + List