https://github.com/romanqed/jct
Lightweight Java implementation of an operation cancellation mechanism, similar to C#'s CancellationToken.
https://github.com/romanqed/jct
abort async cancel cancellation cancellation-api cancellationtoken concurrency interrupt java lightweight multithreading token
Last synced: 3 months ago
JSON representation
Lightweight Java implementation of an operation cancellation mechanism, similar to C#'s CancellationToken.
- Host: GitHub
- URL: https://github.com/romanqed/jct
- Owner: RomanQed
- License: apache-2.0
- Created: 2025-07-27T09:46:57.000Z (3 months ago)
- Default Branch: main
- Last Pushed: 2025-07-27T10:01:57.000Z (3 months ago)
- Last Synced: 2025-07-27T11:42:56.260Z (3 months ago)
- Topics: abort, async, cancel, cancellation, cancellation-api, cancellationtoken, concurrency, interrupt, java, lightweight, multithreading, token
- Language: Java
- Homepage:
- Size: 64.5 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# jct [](https://repo1.maven.org/maven2/com/github/romanqed/jct)
Lightweight Java implementation of an operation cancellation mechanism, similar to C#'s CancellationToken.
Designed to be minimal, composable, and runtime-agnostic — works well with standard Java concurrency tools and third-party frameworks.## Getting Started
To use this library, you will need:
* Java 11 or higher
* Maven or Gradle## Features
* Immutable, thread-safe `CancelToken` abstraction
* Simple and lightweight cancellation source (`CancelSource`)
* Composable tokens (linked or combined)
* Integration with `CompletableFuture` and any asynchronous workflows
* Memory-friendly: `CancelSource` supports reuse via `reset()`## Installing
### Gradle dependency
```groovy
dependencies {
implementation group: 'com.github.romanqed', name: 'jct', version: '1.2.0'
}
```### Maven dependency
```xml
com.github.romanqed
jct
1.2.0
```
## Usage Examples
### Creating a cancelable operation
```java
CancelSource source = Cancellation.source();
CancelToken token = source.token();CompletableFuture future = token.onCancelled().thenRun(() -> {
System.out.println("Cancelled!");
});// Later:
source.cancel();
```### Combining tokens
```java
CancelToken token1 = Cancellation.source().token();
CancelToken token2 = Cancellation.source().token();CancelToken combined = Cancellation.combinedToken(token1, token2);
combined.onCancelled().thenRun(() -> {
System.out.println("Any of the tokens was cancelled");
});
```### Reusing a source
```java
CancelSource source = Cancellation.source();// Use source.token() in one task
runCancellableTask(source.token());// Cancel and reset
source.cancel();
source.reset();// Reuse for another task
runCancellableTask(source.token());
```### Empty token (never cancels)
```java
CancelToken empty = Cancellation.emptyToken();empty.onCancelled().thenRun(() -> {
// Will never be invoked
});
```## Built With
* [Gradle](https://gradle.org) - Dependency management
## Authors
* **[RomanQed](https://github.com/RomanQed)** - *Main work*
See also the list of [contributors](https://github.com/RomanQed/jfunc/contributors)
who participated in this project.## License
This project is licensed under the Apache License 2.0 - see the [LICENSE](LICENSE) file for details