core.async and Virtual Threads
core.async 1.9.847-alpha3 is now available. This release reverts the core.async virtual thread implementation added in alpha2 , and provides a new implementation ( ASYNC-272 ). Threads must block while waiting on I/O operations to complete. "Parking" allows the platform to unmount and free the underlying thread resource while waiting. This allows users to write "normal" straight line code (without callbacks) while consuming fewer platform resources. io-thread execution context io-thread was added in a previous core.async release and is a new execution context for running both blocking channel operations and blocking I/O operations (which are not supported in go ). Parking operations are not allowed in io-thread (same as the thread context). io-thread uses the :io executor pool, which will now use virtual threads, when available. If used in Java without virtual threads (< 21), io-thread continues to run in a cached thread pool with platform threads. With this change, all blocking operations in io-thread park without consuming a platform thread on Java 21+. go blocks Clojure core.async go blocks use an analyzer to rewrite code with inversion of control specifically for channel parking operations (the ! async ops like >! ). Other blocking operations ( !! channel ops or arbitrary I/O ops) are not allowed. Additionally, go blocks are automatically collected if the channels they depend on are collected (and parking can never progress). The Java 21 virtual threads feature implements I/O parking in the Java platform itself - that capability is a superset of what go blocks provide by

core.async 1.9.847-alpha3 has been released, marking a significant update in the virtual thread implementation. This version reverts the changes introduced in alpha2 and introduces a new implementation based on ASYNC-272. The core innovation lies in the ability to handle blocking I/O operations more efficiently, allowing users to write straightforward, linear code without the need for callbacks while reducing platform resource consumption.
In this release, the io-thread execution context has been enhanced. Introduced in a previous core.async release, io-thread is designed to execute both blocking channel operations and I/O operations that are not supported in the go blocks. Importantly, parking operations are not permitted in io-thread, adhering to the same constraints as the thread context. The io-thread execution context now utilizes the :io executor pool, which will leverage virtual threads when available.
For Java environments without virtual threads (pre-Java 21), io-thread continues to operate within a cached thread pool, utilizing platform threads. With the introduction of virtual threads in Java 21 and later, all blocking operations within io-thread will park without consuming a platform thread. This optimization ensures that applications can handle I/O-bound tasks more efficiently, reducing the overhead associated with maintaining a large number of threads.
The go blocks in Clojure core.async have also been updated to integrate with this new virtual thread implementation. Go blocks use an analyzer to rewrite code with inversion of control specifically for channel parking operations, such as the ! async ops like >!. However, other blocking operations, such as !! channel ops or arbitrary I/O operations, are not supported within go blocks. Additionally, go blocks are automatically collected if the channels they depend on are collected, ensuring that parking can never progress in such cases.
Java 21's virtual threads feature introduces I/O parking capabilities natively within the Java platform, which is a superset of the functionality provided by go blocks. While virtual threads support all blocking I/O operations, they differ from go blocks in that they must terminate ordinarily and will keep referenced resources alive until they do. This distinction in semantics means that go blocks remain unchanged and continue to use the go analyzer and run on platform threads.
If developers wish to leverage the benefits and constraints of virtual threads, they can convert go blocks to io-thread execution context. This change allows them to take advantage of the improved efficiency and resource management offered by virtual threads while adhering to the specific constraints of the io-thread context.
In summary, core.async 1.9.847-alpha3 introduces a new virtual thread implementation that enhances the handling of blocking I/O operations, enabling developers to write more efficient and linear code. The io-thread execution context has been refined to better utilize virtual threads, and go blocks have been updated to integrate with this new approach. This release represents a significant step forward in optimizing I/O-bound applications and demonstrates the potential of virtual threads in Java 21 and later versions.










