Giter Club home page Giter Club logo

Comments (4)

vsilaev avatar vsilaev commented on May 26, 2024

It's not related to the library, but you may do smth. like this:

        CountDownLatch doneInAnyWay = new CountDownLatch(1);
        Promise<Void> task = CompletableTask.runAsync(() -> {
            try{
                //...
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                throw new CompletionException(e);
            } finally {
                doneInAnyWay.countDown();
            }
        }, EXECUTOR);
        task.cancel(true);
        //wait for interruption OR normal OR exceptional completion
        doneInAnyWay.await();

from tascalate-concurrent.

lelmarir avatar lelmarir commented on May 26, 2024

Thank you,
I've created this implementation i'd like to share:
I've not managed to create a InterruptableTaks, but only an InterruptableFuture that will suffice for my project

import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Supplier;

import net.tascalate.concurrent.CompletableTask;
import net.tascalate.concurrent.Promise;

public class InterruptableFuture<T> extends CompletableFuture<T> {

	public static InterruptableFuture<Void> runAsync(Runnable runnable, Executor executor) {
		return submit(() -> {
			runnable.run();
			return null;
		}, executor);
	}

	public static <U> InterruptableFuture<U> submit(Callable<U> callable, Executor executor) {
		return new InterruptableFuture<>(callable, executor);
	}

	public static <U> InterruptableFuture<U> supplyAsync(Supplier<U> supplier, Executor executor) {
		return new InterruptableFuture<>(supplier::get, executor);
	}

	private final Promise<Void> task;
	private final AtomicBoolean running = new AtomicBoolean(false);

	protected InterruptableFuture(Callable<T> callable, Executor executor) {
		task = CompletableTask.runAsync(() -> {
			if (running.compareAndSet(false, true)) {
				try {
					complete(callable.call());
				} catch (Exception e) {
					completeExceptionally(e);
				}
			}
			// if tryLock fail cancel has been already called
		}, executor);
	}

	@Override
	public boolean cancel(boolean mayInterruptIfRunning) {
		boolean cancelled = task.cancel(mayInterruptIfRunning);
		if (cancelled && running.compareAndSet(false, true)) {
			// task not started yet
			super.cancel(false);
		}
		return cancelled;
	}
}

from tascalate-concurrent.

vsilaev avatar vsilaev commented on May 26, 2024

AtomicBoolean.compareAndSet will be a better choice than ReentantLock, IMHO.

from tascalate-concurrent.

lelmarir avatar lelmarir commented on May 26, 2024

You're right! I've updated the code 👍

from tascalate-concurrent.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.