Jai and Veeru were working late at night in their dimly lit office. The coffee cups were piling up, and Veeru was staring at his laptop screen, confused and frustrated.
“Jai, something weird is happening!” he groaned.
Jai, half-asleep, barely looked up. “Is it your buggy code again?”
Veeru shook his head. “No! Our app is supposed to log user activity, but somehow… data keeps disappearing!”
Jai frowned. “What do you mean? Logs don’t just vanish!”
Veeru pointed at the screen. “Look! Here’s a log of a user logging in… but when the request reaches the backend, poof! The data is gone!”
Jai’s eyes widened. This wasn’t just a bug. This was a ghost in the repository.
The Problem: Lost Data in Multi-Threaded Requests
Veeru’s code was using ThreadLocal to store temporary data for each request:
private static final ThreadLocal<User> currentUser = new ThreadLocal<>();
public void handleRequest(User user) {
currentUser.set(user);
process();
}
public void process() {
System.out.println("Processing user: " + currentUser.get().name());
}
At first, it worked fine. But when the system scaled up and multiple requests came in, logs showed missing user data!
Veeru scratched his head. “Why is this happening, Jai?”
Jai sighed. “Because ThreadLocal is broken in virtual threads!”
The Fix: Java 21’s Scoped Values
“Veeru, let me introduce you to Scoped Values — the modern replacement for ThreadLocal!” Jai said, rewriting the code.
private static final ScopedValue<User> currentUser = ScopedValue.newInstance();
public void handleRequest(User user) {
ScopedValue.where(currentUser, user).run(() -> process());
}
public void process() {
System.out.println("Processing user: " + currentUser.get().name());
}
Veeru’s jaw dropped. “Wait… so Scoped Values don’t break in Virtual Threads?”
Jai nodded. “Exactly! They are immutable, memory-efficient, and safe for concurrency. No more lost data!”
The Repository’s Dark Secret
Just as Jai and Veeru were celebrating their victory, an alert flashed on the screen.
“Unauthorized commit detected. Unknown changes pushed to the repository.”
Veeru gulped. “Jai… did you push any code?”
Jai shook his head. “No… and we’re the only ones with access!”
They checked the repository logs. A mysterious commit had overwritten part of their code — the same part handling user logs!
Jai narrowed his eyes and whispered, “Could it be… a supply chain attack?”
Veeru’s face turned pale. “You mean like the Log4j exploit?”
Jai nodded. “Exactly. If someone injected malicious code into our dependencies, we might have a security vulnerability in our system right now!”
Veeru’s hands trembled as he reached for his keyboard. “Jai, we need to check our dependencies and Maven repositories — right now!”
📖 To be continued… 🚀
💡Key Takeaways from This Chapter
✔ ThreadLocal is outdated — Switch to Scoped Values for better concurrency.
✔ Scoped Values — Immutable, thread-safe, and designed for modern Java applications.
✔ Data Consistency — Ensure no data is lost in multi-threaded environments.
✔ Supply Chain Attacks — Stay updated with security best practices in Java.
Top comments (0)