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

com.github.romanqed.jutils.concurrent.ThreadTaskFabric Maven / Gradle / Ivy

Go to download

A set of simple utilities for implementing missing functionality in different versions of Java.

There is a newer version: 4.0.0
Show newest version
package com.github.romanqed.jutils.concurrent;

import com.github.romanqed.jutils.util.Checks;

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

public class ThreadTaskFabric implements TaskFabric {
    protected ExecutorService executor;

    public ThreadTaskFabric(ExecutorService executor) {
        this.executor = Checks.requireNonNullElse(executor, Executors.newCachedThreadPool());
    }

    public ThreadTaskFabric() {
        this(null);
    }

    @Override
    public  Task createTask(Callable action) {
        return new AbstractTask() {
            @Override
            public T call() throws Exception {
                return action.call();
            }

            @Override
            public ExecutorService getExecutor() {
                return executor;
            }
        };
    }


    @Override
    public ExecutorService getExecutor() {
        return executor;
    }

    @Override
    public boolean hasExecutor() {
        return executor != null;
    }

    @Override
    public void close() {
        executor.shutdown();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy