Skip to content

Add ThreadingTools.interruptOnCancel #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/com/tunnelvisionlabs/util/concurrent/ThreadingTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
public enum ThreadingTools {
;

private static final Disposable EMPTY_DISPOSABLE = () -> { };

// /// <summary>
// /// Optimistically performs some value transformation based on some field and tries to apply it back to the field,
// /// retrying as many times as necessary until no other thread is manipulating the same field.
Expand Down Expand Up @@ -49,6 +51,18 @@ public enum ThreadingTools {
// return true;
// }

public static Disposable interruptOnCancel(@NotNull CancellationToken cancellationToken) {
return interruptOnCancel(Thread.currentThread(), cancellationToken);
}

public static Disposable interruptOnCancel(@NotNull Thread thread, @NotNull CancellationToken cancellationToken) {
if (!cancellationToken.canBeCancelled()) {
return EMPTY_DISPOSABLE;
}

return cancellationToken.register(thread::interrupt);
}

/**
* Wraps a future with one that will complete as canceled based on a cancellation token, allowing someone to await
* a task but be able to break out early by canceling the token.
Expand Down
29 changes: 29 additions & 0 deletions test/com/tunnelvisionlabs/util/concurrent/AsyncTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
package com.tunnelvisionlabs.util.concurrent;

import com.tunnelvisionlabs.util.validation.NotNull;
import java.time.Duration;
import java.util.concurrent.CancellationException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
Expand Down Expand Up @@ -143,4 +145,31 @@ public Void getResult() {

asyncTest.join();
}

@NotNull
private CompletableFuture<Void> sleepingMethodAsync(@NotNull Duration duration, @NotNull CancellationToken cancellationToken) {
return Async.runAsync(() -> {
try (Disposable registration = ThreadingTools.interruptOnCancel(cancellationToken)) {
try {
Thread.sleep(duration.toMillis());
} catch (InterruptedException ex) {
return Futures.completedCancelled();
}
}

return Futures.completedNull();
});
}

@Test
public void testCancelSleep() {
CompletableFuture<Void> asyncTest = Async.runAsync(() -> {
return Async.awaitAsync(AsyncAssert.assertCancelsAsync(() -> {
CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(Duration.ofMillis(20));
return sleepingMethodAsync(Duration.ofMinutes(5), cancellationTokenSource.getToken());
}));
});

asyncTest.join();
}
}