<commit_before>package javafixes.concurrency;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;

import static java.util.concurrent.Executors.newScheduledThreadPool;
import static java.util.concurrent.TimeUnit.SECONDS;

/**
 * An {@link java.util.concurrent.ScheduledExecutorService ScheduledExecutorService} alternative
 * that allows you to monitor the number of submitted, failed and succeeded task, plus has the
 * ability to wait until all tasks are completed = there are no scheduled or running tasks.
 *
 * @author mtymes
 */
public class Runner extends MonitoringTaskSubmitter {

    /**
     * Constructs a runner with specific number of executor thread.
     *
     * @param threadCount number of executor threads
     */
    public Runner(int threadCount) {
        super(newScheduledThreadPool(threadCount));
    }

    /**
     * Factory method to create a {@code Runner}.
     *
     * @param threadCount number of executor threads
     * @return new {@code Runner}
     */
    public static Runner runner(int threadCount) {
        return new Runner(threadCount);
    }

    @Override
    public Runner waitTillDone() {
        super.waitTillDone();
        return this;
    }

    /**
     * Sends a shutdown signal to the runner.
     *
     * @return this {@code Runner} instance
     * @see ExecutorService#shutdown()
     */
    public Runner shutdown() {
        executor.shutdown();
        return this;
    }

    /**
     * Sends a shutdownNow signal to the runner.
     *
     * @return this {@code Runner} instance
     * @see ExecutorService#shutdownNow()
     */
    public Runner shutdownNow() {
        executor.shutdownNow();
        return this;
    }

    /**
     * Blocks until all tasks have completed execution after a shutdown
     * request, or the timeout occurs, or the current thread is
     * interrupted, whichever happens first.
     *
     * @param timeout the maximum time to wait
     * @param unit    the time unit of the timeout argument
     * @return this {@code Runner} instance
     * @throws RuntimeException wrapping an {@code InterruptedException} if interrupted while waiting
     */
    public Runner awaitTermination(long timeout, TimeUnit unit) {
        try {
            executor.awaitTermination(timeout, unit);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return this;
    }

    /**
     * Calls {@link #shutdown()} and {@link #awaitTermination(long, TimeUnit)} methods in sequence
     *
     * @param timeout the maximum time to wait
     * @param unit    the time unit of the timeout argument
     * @return this {@code Runner} instance
     * @throws RuntimeException wrapping an {@code InterruptedException} if interrupted while waiting
     */
    public Runner shutdownAndAwaitTermination(long timeout, TimeUnit unit) {
        return shutdown().awaitTermination(timeout, unit);
    }

    /**
     * Calls {@link #shutdown()} and {@link #awaitTermination(long, TimeUnit)} methods in sequence
     * where the timeout is set to 5 seconds.
     *
     * @return this {@code Runner} instance
     * @throws RuntimeException wrapping an {@code InterruptedException} if interrupted while waiting
     */
    public Runner shutdownAndAwaitTermination() {
        return shutdownAndAwaitTermination(5, SECONDS);
    }

    /**
     * Calls {@link #shutdownNow()} and {@link #awaitTermination(long, TimeUnit)} methods in sequence
     *
     * @param timeout the maximum time to wait
     * @param unit    the time unit of the timeout argument
     * @return this {@code Runner} instance
     * @throws RuntimeException wrapping an {@code InterruptedException} if interrupted while waiting
     */
    public Runner shutdownNowAndAwaitTermination(long timeout, TimeUnit unit) {
        return shutdownNow().awaitTermination(timeout, unit);
    }

    /**
     * Calls {@link #shutdownNow()} and {@link #awaitTermination(long, TimeUnit)} methods in sequence
     * where the timeout is set to 5 seconds.
     *
     * @return this {@code Runner} instance
     * @throws RuntimeException wrapping an {@code InterruptedException} if interrupted while waiting
     */
    public Runner shutdownNowAndAwaitTermination() {
        return shutdownNowAndAwaitTermination(5, SECONDS);
    }
}<commit_msg>mtymes: added finalization method into Runner to shutdown the executor on garbage collection<commit_after>package javafixes.concurrency;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;

import static java.util.concurrent.Executors.newScheduledThreadPool;
import static java.util.concurrent.TimeUnit.SECONDS;

/**
 * An {@link java.util.concurrent.ScheduledExecutorService ScheduledExecutorService} alternative
 * that allows you to monitor the number of submitted, failed and succeeded task, plus has the
 * ability to wait until all tasks are completed = there are no scheduled or running tasks.
 *
 * @author mtymes
 */
public class Runner extends MonitoringTaskSubmitter {

    /**
     * Constructs a runner with specific number of executor thread.
     *
     * @param threadCount number of executor threads
     */
    public Runner(int threadCount) {
        super(newScheduledThreadPool(threadCount));
    }

    /**
     * Factory method to create a {@code Runner}.
     *
     * @param threadCount number of executor threads
     * @return new {@code Runner}
     */
    public static Runner runner(int threadCount) {
        return new Runner(threadCount);
    }

    @Override
    public Runner waitTillDone() {
        super.waitTillDone();
        return this;
    }

    /**
     * Sends a shutdown signal to the runner.
     *
     * @return this {@code Runner} instance
     * @see ExecutorService#shutdown()
     */
    public Runner shutdown() {
        executor.shutdown();
        return this;
    }

    /**
     * Sends a shutdownNow signal to the runner.
     *
     * @return this {@code Runner} instance
     * @see ExecutorService#shutdownNow()
     */
    public Runner shutdownNow() {
        executor.shutdownNow();
        return this;
    }

    /**
     * Blocks until all tasks have completed execution after a shutdown
     * request, or the timeout occurs, or the current thread is
     * interrupted, whichever happens first.
     *
     * @param timeout the maximum time to wait
     * @param unit    the time unit of the timeout argument
     * @return this {@code Runner} instance
     * @throws RuntimeException wrapping an {@code InterruptedException} if interrupted while waiting
     */
    public Runner awaitTermination(long timeout, TimeUnit unit) {
        try {
            executor.awaitTermination(timeout, unit);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return this;
    }

    /**
     * Calls {@link #shutdown()} and {@link #awaitTermination(long, TimeUnit)} methods in sequence
     *
     * @param timeout the maximum time to wait
     * @param unit    the time unit of the timeout argument
     * @return this {@code Runner} instance
     * @throws RuntimeException wrapping an {@code InterruptedException} if interrupted while waiting
     */
    public Runner shutdownAndAwaitTermination(long timeout, TimeUnit unit) {
        return shutdown().awaitTermination(timeout, unit);
    }

    /**
     * Calls {@link #shutdown()} and {@link #awaitTermination(long, TimeUnit)} methods in sequence
     * where the timeout is set to 5 seconds.
     *
     * @return this {@code Runner} instance
     * @throws RuntimeException wrapping an {@code InterruptedException} if interrupted while waiting
     */
    public Runner shutdownAndAwaitTermination() {
        return shutdownAndAwaitTermination(5, SECONDS);
    }

    /**
     * Calls {@link #shutdownNow()} and {@link #awaitTermination(long, TimeUnit)} methods in sequence
     *
     * @param timeout the maximum time to wait
     * @param unit    the time unit of the timeout argument
     * @return this {@code Runner} instance
     * @throws RuntimeException wrapping an {@code InterruptedException} if interrupted while waiting
     */
    public Runner shutdownNowAndAwaitTermination(long timeout, TimeUnit unit) {
        return shutdownNow().awaitTermination(timeout, unit);
    }

    /**
     * Calls {@link #shutdownNow()} and {@link #awaitTermination(long, TimeUnit)} methods in sequence
     * where the timeout is set to 5 seconds.
     *
     * @return this {@code Runner} instance
     * @throws RuntimeException wrapping an {@code InterruptedException} if interrupted while waiting
     */
    public Runner shutdownNowAndAwaitTermination() {
        return shutdownNowAndAwaitTermination(5, SECONDS);
    }

    protected void finalize() {
        this.shutdownNow();
    }
}
{'fraction_non_alphanumeric': 0.08527219335893381, 'fraction_numerical': 0.0009035464196973119, 'mean_word_length': 2.9094922737306845, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3010466', 'n_tokens_mistral': 2348, 'n_tokens_neox': 2304, 'n_words': 808}