verhas / vtstream

Virtual Thread Stream Library

Home Page:https://github.com/verhas/vtstream

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Virtual Thread Stream Library

The Virtual Thread Stream (VTS) library, contained within the javax0.vtstream package, leverages the power of virtual threads introduced in Java to provide a high-performance, easy-to-use streaming API that supports parallel processing with minimal overhead.

Overview

ThreadedStream implements the Java Stream interface, providing a seamless integration with the existing stream API. This compatibility allows developers to easily adopt virtual thread-based parallelism in their applications, leveraging the rich set of stream operations such as map, filter, sorted, and more, without having to learn a new API.

Features

  • Parallelism with Virtual Threads: Utilize virtual threads for efficient parallel processing of stream operations, reducing the overhead associated with traditional multi-threading.

  • Flexible Stream Operations: Supports a wide range of operations, including map, filter, distinct, limit, skip, and custom operations through the Command class.

  • Seamless Integration: Easily integrates with existing Java codebases, allowing for the parallel processing of streams with minimal changes to the code.

Usage

Creating a ThreadedStream

To create a ThreadedStream, you can use the static method threaded provided by the ThreadedStream class:

Stream<String> sourceStream = Stream.of("apple", "banana", "cherry");
ThreadedStream<String> threadedStream = ThreadedStream.threaded(sourceStream);

This creates a ThreadedStream that can process elements in parallel.

Applying Operations

Operations such as map, filter, and distinct are applied through the Command subclasses:

ThreadedStream<String> filteredStream = threadedStream.filter(s -> s.startsWith("a"));
ThreadedStream<String> mappedStream = filteredStream.map(String::toUpperCase);

Each operation returns a new ThreadedStream instance that represents the stream after the operation has been applied.

Terminal Operations

To execute the stream operations and collect results, you can use terminal operations like forEach, collect, or reduce:

List<String> result = mappedStream.collect(Collectors.toList());

This will execute the stream pipeline in parallel and collect the results into a list.

Closing Thoughts

The Virtual Thread Stream library offers a powerful and flexible way to perform parallel stream processing in Java, making it easier to write efficient and scalable applications. By leveraging virtual threads, it addresses many of the common challenges associated with parallelism, such as overhead and complexity.

Readme entirely generated by ChatGPT4.

About

Virtual Thread Stream Library

https://github.com/verhas/vtstream

License:Apache License 2.0


Languages

Language:Java 100.0%