Skip to content

About

Advanced Multithreading concepts in Java

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 

Repository files navigation

🧅 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

About

Advanced Multithreading concepts in Java

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages