All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.smallibs.concurrent.execution.impl.ExecutorImpl Maven / Gradle / Ivy

There is a newer version: 0.11.0
Show newest version
/*
 * HPAS
 * https://github.com/d-plaindoux/hpas
 *
 * Copyright (c) 2016-2017 Didier Plaindoux
 * Licensed under the LGPL2 license.
 */

package org.smallibs.concurrent.execution.impl;

import org.smallibs.concurrent.execution.Executor;
import org.smallibs.concurrent.promise.Promise;
import org.smallibs.concurrent.promise.impl.RunnablePromise;
import org.smallibs.data.Unit;

import java.util.Objects;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;

/**
 * Asynchronous execution media
 */
public final class ExecutorImpl implements Executor {

    /**
     * Underlying executor service
     */
    private final ExecutorService executorService;

    /**
     * Constructor
     *
     * @param executorService The executor service used for asynchronous operation
     */
    public ExecutorImpl(ExecutorService executorService) {
        this.executorService = executorService;
    }

    @Override
    public  Promise async(Callable task) {
        Objects.requireNonNull(task);

        final RunnablePromise runnablePromise = new RunnablePromise<>(task);

        this.executorService.execute(runnablePromise);
        return runnablePromise;
    }

    @Override
    public Promise async(RunnableWithError task) {
        Objects.requireNonNull(task);

        final RunnablePromise runnablePromise = new RunnablePromise<>(() -> {
            task.run();
            return Unit.unit;
        });

        this.executorService.execute(runnablePromise);
        return runnablePromise;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy