Skip to content
/ jct Public

Lightweight Java implementation of an operation cancellation mechanism, similar to C#'s CancellationToken.

License

Notifications You must be signed in to change notification settings

RomanQed/jct

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

jct 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

dependencies {
    implementation group: 'com.github.romanqed', name: 'jct', version: '1.2.1'
}

Maven dependency

<dependencies>
    <dependency>
        <groupId>com.github.romanqed</groupId>
        <artifactId>jct</artifactId>
        <version>1.2.1</version>
    </dependency>
</dependencies>

Usage Examples

Creating a cancelable operation

CancelSource source = Cancellation.source();
CancelToken token = source.token();

CompletableFuture<Void> future = token.onCancelled().thenRun(() -> {
    System.out.println("Cancelled!");
});

// Later:
source.cancel();

Combining tokens

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

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)

CancelToken empty = Cancellation.emptyToken();

empty.onCancelled().thenRun(() -> {
    // Will never be invoked
});

Built With

  • Gradle - Dependency management

Authors

See also the list of contributors who participated in this project.

License

This project is licensed under the Apache License 2.0 - see the LICENSE file for details

About

Lightweight Java implementation of an operation cancellation mechanism, similar to C#'s CancellationToken.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages