SShortSingh.
Back to feed

Java 21 introduces Scoped Values as a safer alternative to ThreadLocal

0
·1 views

Java's ThreadLocal class, available since version 1.2, allows threads to store independent copies of variables but carries risks such as memory leaks and data cross-contamination when threads are reused in pools. With Java 21's introduction of Virtual Threads, these problems are amplified, as millions of lightweight threads can each hold their own ThreadLocal copies, potentially overwhelming heap memory. To address these issues, Java 21 introduced Scoped Values (JEP 446), which bind data to a specific block of code rather than to the thread itself. Unlike ThreadLocal, Scoped Values are immutable, require no manual cleanup, and automatically release data when the code block exits. Developers are advised to prefer Scoped Values in new code, especially when working with virtual threads, while exercising extra caution if continuing to use ThreadLocal.

Read the full story at DEV Community

This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)

Log in to join the discussion and vote.

Log in

Related stories

0
ProgrammingDEV Community ·

How Poor Document Chunking Causes RAG-Based AI Tools to Return Misleading Answers

Retrieval-augmented generation (RAG) systems, widely used to help AI answer questions from company documents, can return information that is technically accurate but contextually misleading. A software team discovered this when their RAG-powered tool cited a revenue figure from a Fortune 500 client's report without recognising it was a projected number, not actual revenue, because the qualifying sentence was stored in a separate data chunk. An audit of 50 problematic queries revealed recurring failures, including table rows retrieved without column headers and contract clauses stripped of their governing definitions. The core issue lies in how documents are processed: standard RAG pipelines break files into fixed-size text chunks, discarding structural cues like headings, table headers, and cross-references before the AI ever reads them. This architectural flaw means the AI has no way to understand the broader context of the fragments it retrieves, leading to confident but incomplete responses.

0
ProgrammingDEV Community ·

airCloset CTO: Switching CI Runners Cut GitHub Actions Costs by 75%

airCloset CTO Ryan reports that migrating GitHub Actions runners from GitHub-hosted to third-party providers reduced per-run CI costs to roughly one-quarter of the original expense. The company made two successive migrations — first to Blacksmith, then to Namespace — each requiring only a single line of code change in the workflow configuration. Beyond cost savings, the move cut the slowest runs (p90 latency) by 37% and eliminated silent hang failures entirely. The team notes the savings are only relevant for organizations that have exceeded GitHub's free tier, as repositories within the 3,000 free monthly minutes have no reason to migrate. With AI agents increasingly driving development and multiplying both run counts and task complexity, the CTO warns that unmanaged CI costs will grow steadily unless addressed.

0
ProgrammingDEV Community ·

NERA Encyclopedia Launches as Digital Infrastructure for Black Knowledge and Identity

NERA (Negrooverso Encyclopedia) is a Progressive Web Application designed as a dedicated digital space to systematize Black history, science, culture, and cosmology that has long been absent or misrepresented in mainstream knowledge platforms. The project was built with a Desktop First philosophy to encourage deep study and immersive learning rather than quick mobile consumption. At its core, NERA Academy offers structured learning paths where users, called Breakers, engage actively with interconnected knowledge rather than passively consuming content. Each user receives a unique @this.ngv identity, framed as a form of digital ownership within the Negrooverso ecosystem. The initiative aims to close what its creator describes as a critical cultural-technical debt — the algorithmic invisibility caused by the historic lack of organized, accessible Black knowledge in the data sets shaping AI and digital futures.

0
ProgrammingDEV Community ·

Developer Builds LLM Trading Bot After Reading 77 Research Papers

A developer based in Wrocław, Poland spent late 2024 building an AI-powered trading bot after repeated failures using conventional LLM prompt-based approaches found in online tutorials. Instead of feeding raw numbers to a language model, he shifted to visual inference by sending 4K candlestick charts with five technical indicators directly to Google Gemini for pattern analysis. After testing his own hand-coded pattern detection algorithms against the AI's visual reads, he found the AI significantly outperformed his code, leading him to delete 900 lines of custom logic. He built a full indicator engine from scratch using NumPy and Numba, compiling over 50 technical indicators to machine code for microsecond-level performance on a standard desktop CPU. The bot uses a single structured prompt that requires the model to argue both bullish and bearish cases before reaching a decision, reducing API costs compared to multi-agent frameworks.

Java 21 introduces Scoped Values as a safer alternative to ThreadLocal · ShortSingh