Skip to content

Latest commit

Β 

History

History
80 lines (50 loc) Β· 2.58 KB

README.md

File metadata and controls

80 lines (50 loc) Β· 2.58 KB

πŸ§… Advanced Multithreading in Java

This project demonstrates advanced multithreading concepts in Java, including thread synchronization, parallel execution, asynchronous computation, and coordination between threads β€” all combined in a single example.


πŸš€ Features

βœ… ExecutorService – Efficient thread pool management

βœ… Callable & Future – Parallel computation with return values

βœ… ReentrantLock – Thread-safe shared resource access

βœ… CountDownLatch – Coordinating multiple threads

βœ… CompletableFuture – Asynchronous, non-blocking computation

βœ… Graceful shutdown – Proper thread pool termination


🧠 Concepts Covered

Concept Description
ExecutorService Manages a pool of worker threads instead of creating new threads manually.
Callable & Future Allows threads to return results and handle exceptions.
ReentrantLock Provides explicit locking for shared resource protection.
CountDownLatch Helps coordinate multiple threads to wait for each other’s completion.
CompletableFuture Enables async programming with chained callbacks.
Graceful Shutdown Ensures no running tasks are abruptly terminated.

🧩 Code Overview

πŸ’° BankAccount (Shared Resource)

A thread-safe class that handles deposit and withdrawal operations using a ReentrantLock to prevent race conditions.

βš™οΈ Main Class

  • Submits multiple Callable tasks using an ExecutorService.
  • Uses a CountDownLatch to synchronize threads.
  • Performs an asynchronous interest calculation with CompletableFuture.
  • Demonstrates Future usage for retrieving computed values.

πŸ—² Sample Output

pool-1-thread-1 deposited 200.0 | Balance: 1200.0
pool-1-thread-2 withdrew 100.0 | Balance: 1100.0
pool-1-thread-3 deposited 300.0 | Balance: 1400.0
pool-1-thread-4 withdrew 500.0 | Balance: 900.0

All basic transactions completed.

Calculating interest asynchronously...
Interest calculated: 45.0
main deposited 45.0 | Balance: 945.0

Final balance after interest: 945.0

Executor shutdown gracefully.

🀩 Topics Demonstrated

  • Multithreading fundamentals
  • Concurrency utilities (java.util.concurrent)
  • Thread safety and synchronization
  • Modern async programming in Java