65 lines
3.0 KiB
Markdown
65 lines
3.0 KiB
Markdown
# Assignment Report: Multithreading Basics
|
||
### Course: Advanced Programming
|
||
|
||
## Assignment: Eighth Assignment – Multithreading Basics
|
||
|
||
Project: Simulated Download Manager
|
||
|
||
1. Theoretical Questions
|
||
1.1 Difference Between start() and run()
|
||
In Java, the run() method contains the code that a thread should execute. However, calling run() directly does not create a new thread; it works like a normal method call in the current thread.
|
||
|
||
When start() is called, the JVM creates a new thread, which then executes the run() method internally.
|
||
|
||
1.2 Daemon Threads
|
||
A daemon thread is a background thread. The JVM does not wait for daemon threads to finish. When all user threads finish, the JVM stops the program even if daemon threads are still running.
|
||
|
||
If setDaemon(true) is removed, the thread becomes a normal user thread, and the JVM will wait for it to complete.
|
||
|
||
1.3 Lambda Expressions for Threads
|
||
A lambda expression is a concise way to implement a functional interface like Runnable. Since Runnable has only one method (run()), we can use:
|
||
|
||
() -> { ... } instead of creating a whole new class.
|
||
|
||
2. Practical Implementation
|
||
2.1 Project Overview
|
||
This project simulates a download manager where a file is divided into chunks, and each chunk is downloaded concurrently by separate worker threads.
|
||
|
||
2.2 DownloadWorker
|
||
The DownloadWorker class implements Runnable. It simulates the download of a single chunk by:
|
||
|
||
Using random step sizes for download progress.
|
||
Sleeping for random delays to simulate network latency.
|
||
Updating its chunk status until the download is complete.
|
||
2.3 ChunkStatus
|
||
This class stores the state of each chunk, including its ID, size, and downloaded amount. Variables are marked as volatile to ensure visibility across different threads.
|
||
|
||
2.4 ProgressMonitor
|
||
The ProgressMonitor runs as a background thread to periodically check and print:
|
||
|
||
Individual chunk progress.
|
||
Total download percentage.
|
||
A visual Progress Bar.
|
||
Average download speed and ETA.
|
||
2.5 Main Class
|
||
The Main class coordinates the process:
|
||
|
||
Reads configuration.
|
||
Initializes chunks and worker threads.
|
||
Starts the monitor and workers.
|
||
Uses join() to wait for all threads to finish before printing the final report.
|
||
3. Bonus Features
|
||
3.1 Progress Bar & ETA
|
||
A visual progress bar was added to the console output. The program also calculates the current speed in MB/s and estimates the remaining time (ETA) based on that speed.
|
||
|
||
3.2 Sequential vs Multithreaded Comparison
|
||
The program compares running the workers sequentially (using .run()) versus concurrently (using .start()). It calculates the Speedup to show how much faster multithreading is for this task.
|
||
|
||
4. Execution Instructions
|
||
To compile and run the project, use the following commands:
|
||
|
||
bash
|
||
mvn compile
|
||
mvn exec:java -Dexec.mainClass="Main"
|
||
5. Conclusion
|
||
This project successfully demonstrates the power of multithreading in Java. By using separate threads for different chunks, the total download time is significantly reduced compared to a sequential approach. |