Virtual Threads Make Blocking Cheap (But Not Free) For years in Java backend development, blocking meant wasted threads, exhausted pools, and collapsed systems under load. With Project Loom (Java 21), that assumption is no longer true - but only if we understand what actually changed. In this article, we will discuss why blocking was expensive, how virtual threads changed the cost model, and where blocking is still dangerous with code examples. https://lnkd.in/gBKg-r_K Subscribe and join 4.3k Java & Spring boot engineers: https://lnkd.in/grH6HSuY
Java 21: Virtual Threads Replace Blocking Costs
More Relevant Posts
-
Virtual Threads Make Blocking Cheap (But Not Free) For years in Java backend development, blocking meant wasted threads, exhausted pools, and collapsed systems under load. With Project Loom (Java 21), that assumption is no longer true - but only if we understand what actually changed. In this article, we will discuss why blocking was expensive, how virtual threads changed the cost model, and where blocking is still dangerous with code examples. https://lnkd.in/g26KjUYc Subscribe and join 4.3k Java & Spring boot engineers: https://lnkd.in/gwiRqWBV
To view or add a comment, sign in
-
📌 Part 2: Processes vs Threads (And Why Java Developers Must Understand This) This is where backend engineers often get confused. 🔹 What is a Process? A Process = Independent program in execution. It has: Own memory space Own heap Own stack Own resources Example: If you run: Chrome IntelliJ MySQL Each is a separate process. In Java: When you start JVM → OS creates a process. 🔹 What is a Thread? Thread = Lightweight unit of execution inside a process. Threads share memory Threads share heap But each has its own stack In Java: Thread t = new Thread(); You are asking OS (through JVM) to create a native thread. 🔥 Why This Matters in Backend Development? Spring Boot app: One process (JVM) Multiple threads (request handling) Tomcat: Uses thread pool Each HTTP request → handled by one thread So when you configure: server.tomcat.threads.max=200 You are indirectly controlling OS-level threads. ⚠️ Context Switching When CPU switches from: Thread A → Thread B OS: Saves registers Saves program counter Loads new context This costs time. Too many threads → Too much context switching → Performance drop. This is why: ExecutorService Thread pools Virtual threads (Project Loom) exist. #Java #JVM #JavaDeveloper #BackendDevelopment #SpringBoot #Concurrency #Multithreading #PerformanceEngineering #MemoryManagement #Threading
To view or add a comment, sign in
-
Everyone knows Core Java… Until the app goes multi-threaded. Now it’s about: • Happens-Before relationship • JMM visibility guarantees • Intrinsic locks vs ReentrantLock • CAS & Atomic primitives • Executor framework & thread pool tuning • Deadlock detection • False sharing • Context switching overhead If you’ve never debugged a race condition at 2 AM, you haven’t really met Java. Single-threaded code proves logic. Multithreaded architecture proves engineering. Concurrency doesn’t forgive assumptions
To view or add a comment, sign in
-
-
Hook Performance and maintainability still derail many Java teams—most often because blocking I/O, thread misuse, and fragile frontend–backend contracts accumulate technical debt faster than features. Core Java Master OOP, collections, generics, exceptions, and the JVM. Understand immutability, value types, and when to choose streams vs imperative loops. Concurrency: thread-safety, synchronized, volatile, java.util.concurrent, and Java 21 virtual threads. Example problems: LRU cache, ATM simulation with secure PIN and transaction history, and prepinsta logical programs
To view or add a comment, sign in
-
Clean and efficient Java code matters. Here’s how Collectors.summingDouble helps you compute total salary using Streams with ease. Link to Video: https://lnkd.in/giFt8G_2
To view or add a comment, sign in
-
If your Java service's p95 latency spikes under load, the usual suspects are blocking threads and unbounded DB connections — not feature complexity. Java 21's virtual threads and structured concurrency let you simplify concurrency without rewriting your whole stack. Combine them with a correctly sized HikariCP pool, proper transaction boundaries, and statement caching in JDBC, and you can cut tail latency while keeping code readable. In Spring Boot, prefer Reactor or virtual-thread-friendly endpoints for IO-heavy flows, tune connectionPool.maxLifetime and validationQuery, use Timeouts at the HTTP client and DB layer, and add robust observability
To view or add a comment, sign in
-
Most teams still scale Java apps by adding threads — and pay for it with memory bloat, GC pauses, and brittle thread-pools. In one migration I reduced median latency by 40% and cut thread footprint by 90% by combining Java 21 virtual threads with focused Spring Boot changes. Problem: Blocking I/O and thread-per-request models hide contention until traffic spikes. The result: long tail latency, OOM risks, and complex tuning. Concrete approach: - Adopt virtual threads
To view or add a comment, sign in
-
Hook If a single thread model is still your go-to for high-concurrency Java services, you’re likely paying for it in latency and operational complexity. Body Java 21 and Spring Boot 3 together give you practical levers to simplify concurrency, reduce memory churn, and make services more observable. Start with virtual threads for request handling: they let you scale to millions of concurrent tasks with minimal code changes. Use records and pattern matching to make DTOs and request parsing concise and bug-resistant. Adopt structured concurrency to group related tasks and improve error handling and shutdown behavior. Practical guidance: keep blocking APIs isolated — use a bounded JDBC pool or migrate critical paths to R2DBC when end-to-end non-blocking is required. Prefer connection pooling and smaller transaction scopes to avoid connection exhaustion. Measure end-to-end latency
To view or add a comment, sign in
-
When should you use Optional in Java? 🧠 Optional is powerful but only when used intentionally. ✅ Use it when a method may or may not return a value. If something can be absent, make that absence explicit. Returning null hides the risk. Returning Optional makes the contract clear. It tells the caller: “Handle this properly.” 🔍 That’s good API design. But don’t overuse it 🚫 • Not as entity fields • Not in DTOs • Not as method parameters • Not as a blanket replacement for every null Optional is a design tool not decoration. Simple rule: Use Optional for return types where absence is valid. Avoid spreading it across your entire data model. Clean code isn’t about using more features. It’s about using the right ones with clarity. ⚙️✨ #Java #BackendDevelopment #CleanCode #SoftwareEngineering #SpringBoot
To view or add a comment, sign in
-
📌 volatile Keyword in Java — Solving the Visibility Problem In multithreading, not all problems are about race conditions. Sometimes the issue is visibility. 🔹 1️⃣ What Is the Visibility Problem? Each thread may cache variables locally. If one thread updates a variable, other threads might not see the updated value immediately.This leads to inconsistent behavior. 🔹 2️⃣ Example Scenario Thread 1: while (!flag) { // waiting } Thread 2: flag = true; Without proper handling, Thread 1 may never see the updated value. 🔹 3️⃣ What volatile Does Declaring a variable as volatile: private volatile boolean flag; Ensures: • Changes are immediately visible to all threads • Value is always read from main memory • No thread-local caching 🔹 4️⃣ Important Limitation volatile does NOT: • Provide atomicity • Prevent race conditions • Replace synchronized It only guarantees visibility. 🔹 5️⃣ When to Use volatile ✔ Simple state flags ✔ One-writer, multiple-reader scenarios ✔ When no compound operations are involved 🧠 Key Takeaway synchronized ensures mutual exclusion. volatile ensures visibility. Both solve different concurrency problems. #Java #Multithreading #Concurrency #Volatile #CoreJava
To view or add a comment, sign in
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development