Recent Posts
Archives

Posts Tagged ‘Concurrency’

PostHeaderIcon [DevoxxGR2024] Butcher Virtual Threads Like a Pro at Devoxx Greece 2024 by Piotr Przybyl

Piotr Przybyl, a Java Champion and developer advocate at Elastic, captivated audiences at Devoxx Greece 2024 with a dynamic exploration of Java 21’s virtual threads. Through vivid analogies, practical demos, and a touch of humor, Piotr demystified virtual threads, highlighting their potential and pitfalls. His talk, rich with real-world insights, offered developers a guide to leveraging this transformative feature while avoiding common missteps. As a seasoned advocate for technologies like Elasticsearch and Testcontainers, Piotr’s presentation was a masterclass in navigating modern Java concurrency.

Understanding Virtual Threads

Piotr began by contextualizing virtual threads within Java’s concurrency evolution. Introduced in Java 21 under Project Loom, virtual threads address the limitations of traditional platform threads, which are costly to create and limited in number. Unlike platform threads, virtual threads are lightweight, managed by a scheduler that mounts and unmounts them from carrier threads during I/O operations. This enables a thread-per-request model, scaling applications to handle millions of concurrent tasks. Piotr likened virtual threads to taxis in a busy city like Athens, efficiently transporting passengers (tasks) without occupying resources during idle periods.

However, virtual threads are not a universal solution. Piotr emphasized that they do not inherently speed up individual requests but improve scalability by handling more concurrent tasks. Their API remains familiar, aligning with existing thread practices, making adoption seamless for developers accustomed to Java’s threading model.

Common Pitfalls and Pinning

A central theme of Piotr’s talk was “pinning,” a performance issue where virtual threads remain tied to carrier threads, negating benefits. Pinning occurs during I/O or native calls within synchronized blocks, akin to keeping a taxi running during a lunch break. Piotr demonstrated this with a legacy Elasticsearch client, using Testcontainers and Toxiproxy to simulate slow network calls. By enabling tracing with flags like -J-DTracePinnThreads, He identified and resolved pinning issues, replacing synchronized methods with modern, non-blocking clients.

Piotr cautioned against misuses like thread pooling or reusing virtual threads, which disrupt their lightweight design. He advocated for careful monitoring using JFR events to ensure threads remain unpinned, ensuring optimal performance in production environments.

Structured Concurrency and Scope Values

Piotr explored structured concurrency, a preview feature in Java 21, designed to eliminate thread leaks and cancellation delays. By creating scopes that manage forks, developers can ensure tasks complete or fail together, simplifying error handling. He demonstrated a shutdown-on-failure scope, where a single task failure cancels all others, contrasting this with the complexity of managing interdependent futures.

Scope Values, another preview feature, offer immutable, one-way thread locals to prevent bugs like data leakage in thread pools. Piotr illustrated their use in maintaining request context, warning against mutability to preserve reliability. These features, he argued, complement virtual threads, fostering robust, maintainable concurrent applications.

Practical Debugging and Best Practices

Through live coding, Piotr showcased how debugging with logging can inadvertently introduce I/O, unmounting virtual threads and degrading performance. He compared this to a concert where logging scatters tasks, reducing completion rates. To mitigate this, he recommended avoiding I/O in critical paths and using structured concurrency for monitoring.

Piotr’s best practices included using framework-specific annotations (e.g., Quarkus, Spring) to enable virtual threads and ensuring tasks are interruptible. He urged developers to test thoroughly, leveraging tools like Testcontainers to simulate real-world conditions. His blog post on testing unpinned threads provides further guidance for practitioners.

Conclusion

Piotr’s presentation was a clarion call to embrace virtual threads with enthusiasm and caution. By understanding their mechanics, avoiding pitfalls like pinning, and leveraging structured concurrency, developers can unlock unprecedented scalability. His engaging analogies and practical demos made complex concepts accessible, empowering attendees to modernize Java applications responsibly. As Java evolves, Piotr’s insights ensure developers remain equipped to navigate its concurrency landscape.

Links:

PostHeaderIcon [PHPForumParis2021] Fiber: The Gateway to Asynchronous PHP – Benoit Viguier

Benoit Viguier, a developer at Bedrock, enthralled the Forum PHP 2021 audience with an exploration of PHP 8.1’s Fiber feature, a groundbreaking step toward asynchronous programming. With a history of discussing async development at AFUP events, Benoit shared early experiments with Fibers, positioning them as a future cornerstone of PHP. His talk blended technical insight with forward-thinking optimism, urging developers to embrace this new paradigm. This post covers three themes: understanding Fibers, practical applications, and the need for standards.

Understanding Fibers

Benoit Viguier introduced Fibers as a low-level feature in PHP 8.1, enabling lightweight, cooperative concurrency. Unlike traditional threading, Fibers allow developers to pause and resume execution without blocking the main thread, ideal for I/O-heavy tasks. Drawing on his work at Bedrock, Benoit explained how Fibers extend PHP’s async capabilities, building on libraries like Amphp and ReactPHP. His clear explanation demystified this cutting-edge feature for the audience.

Practical Applications

Delving into practical use cases, Benoit showcased how Fibers enhance performance in applications like Bedrock’s streaming platforms, such as 6play and Salto. By enabling non-blocking HTTP requests and database queries, Fibers reduce latency and improve user experience. Benoit shared early experiments, noting that while Fibers are not yet production-ready, their potential to streamline async workflows is immense, particularly for high-traffic systems requiring real-time responsiveness.

The Need for Standards

Benoit concluded by advocating for a standardized async ecosystem in PHP. He highlighted recent collaborations between Amphp and ReactPHP teams to propose a PSR standard for Fibers, fostering interoperability. By making libraries “Fiber-ready,” developers can create reusable, non-blocking APIs. Benoit’s vision for a unified async framework, inspired by his work at Bedrock, positions Fibers as a potential “killer feature” for PHP, encouraging community contributions to shape its future.

Links:

PostHeaderIcon [KotlinConf2018] Mastering Concurrency: Roman Elizarov’s Practical Guide to Kotlin Coroutines

Lecturer

Roman Elizarov is a seasoned software developer at JetBrains, with over 17 years of experience in high-performance trading software at Devexperts. An expert in Java and JVM, he teaches concurrent programming at St. Petersburg ITMO and serves as Chief Judge for the Northeastern European Regional Programming Contest. Relevant links: JetBrains Blog (publications); LinkedIn Profile (professional page).

Abstract

This article follows Roman Elizarov’s practical application of Kotlin coroutines to address concurrency challenges. Set in the context of large-scale systems, it examines methodologies for state confinement and communication via channels. The analysis highlights coroutines’ innovations in eliminating shared mutable state, with implications for robust, scalable architectures.

Introduction and Context

Roman Elizarov engaged KotlinConf 2018 attendees with a deep dive into coroutines, building on his prior introductory talk. With a vision for a unified language across distributed systems, Elizarov showcased coroutines as a solution to concurrency without shared mutable state. His examples addressed real-life coordination, set against his experience with high-throughput trading systems processing millions of events per second.

Methodological Approaches to Concurrency

Elizarov demonstrated coroutines confining state to single coroutines, communicating via channels. Each coroutine handles a specific task, receiving input and sending output through channels, avoiding locks. For UI integration, coroutines on the main thread directly update views or report via channels for decoupled architectures. Builders like launch and async orchestrate tasks, while suspend functions enable non-blocking code.

Analysis of Innovations and Features

Coroutines innovate by simplifying async programming. Channels provide fan-out communication, unlike threads’ shared state. Compared to Java’s CompletableFuture, coroutines preserve sequential code structure. Limitations include a learning curve for channel patterns and ensuring proper context management.

Implications and Consequences

Elizarov’s approach implies cleaner, safer concurrency models, reducing bugs in complex systems. It suits UI-driven apps and distributed systems, enhancing scalability. The consequence is a shift toward channel-based designs, though teams must master coroutine semantics.

Conclusion

Elizarov’s practical guide positions coroutines as a cornerstone for modern concurrency, offering a robust alternative to traditional threading models.

Links