<commit_before>/* Copyright 2015 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hayaisoftware.launcher.threading;

import java.util.concurrent.LinkedBlockingQueue;


public class SimpleTaskConsumerManager {

    private final LinkedBlockingQueue<Task> mTasks;
    private int mNumThreadsAlive;
    private boolean mConsumersShouldDie;
    private final Object mWaitingUntilAllFinishedLock;
    private SimpleTaskConsumer[] mSimpleTaskConsumers;

    public SimpleTaskConsumerManager(final int numConsumers) {
        mTasks = new LinkedBlockingQueue<>();
        mWaitingUntilAllFinishedLock = new Object();
        startConsumers(numConsumers);

    }

    private void startConsumers(final int numConsumers) {
        mSimpleTaskConsumers = new SimpleTaskConsumer[numConsumers];
        for (int i = 0; i < numConsumers; i++) {
            mSimpleTaskConsumers[i] = new SimpleTaskConsumer();
            Thread thread = new Thread(mSimpleTaskConsumers[i]);
            thread.start();
        }
    }

    public void addTask(final Task task) {
        if (mConsumersShouldDie) return; //TODO throw exception

        try {
            mTasks.put(task);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void removeAllTasks() {
        mTasks.clear();
    }

    public void destroyAllConsumers(boolean finishCurrentTasks) {
        destroyAllConsumers(finishCurrentTasks, false);
    }


    public void destroyAllConsumers(boolean finishCurrentTasks, boolean blockUntilFinished) {
        if (mConsumersShouldDie) return;


        if (!finishCurrentTasks) removeAllTasks();

        synchronized (mWaitingUntilAllFinishedLock) {
            int threadsToKill = mNumThreadsAlive;
            DieTask dieTask = new DieTask();
            for (int i = 0; i < threadsToKill; i++) {
                mTasks.add(dieTask);
            }
            mConsumersShouldDie = true;
            if (blockUntilFinished) {
                try {
                    mWaitingUntilAllFinishedLock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    @Override
    protected void finalize() throws Throwable {
        //make sure the threads are properly killed
        destroyAllConsumers(false);

        super.finalize();
    }

    public static abstract class Task {
        public abstract void doTask();
    }<commit_msg>fix to previous commit<commit_after>/* Copyright 2015 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hayaisoftware.launcher.threading;

import java.util.concurrent.LinkedBlockingQueue;


public class SimpleTaskConsumerManager {

    private final LinkedBlockingQueue<Task> mTasks;
    private int mNumThreadsAlive;
    private boolean mConsumersShouldDie;
    private final Object mWaitingUntilAllFinishedLock;
    private SimpleTaskConsumer[] mSimpleTaskConsumers;

    public SimpleTaskConsumerManager(final int numConsumers) {
        mTasks = new LinkedBlockingQueue<>();
        mWaitingUntilAllFinishedLock = new Object();
        startConsumers(numConsumers);

    }

    private void startConsumers(final int numConsumers) {
        mSimpleTaskConsumers = new SimpleTaskConsumer[numConsumers];
        for (int i = 0; i < numConsumers; i++) {
            mSimpleTaskConsumers[i] = new SimpleTaskConsumer();
            Thread thread = new Thread(mSimpleTaskConsumers[i]);
            thread.start();
        }
    }

    public void addTask(final Task task) {
        if (mConsumersShouldDie) return; //TODO throw exception

        try {
            mTasks.put(task);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void removeAllTasks() {
        mTasks.clear();
    }

    public void destroyAllConsumers(boolean finishCurrentTasks) {
        destroyAllConsumers(finishCurrentTasks, false);
    }


    public void destroyAllConsumers(boolean finishCurrentTasks, boolean blockUntilFinished) {
        if (mConsumersShouldDie) return;


        if (!finishCurrentTasks) removeAllTasks();

        synchronized (mWaitingUntilAllFinishedLock) {
            mConsumersShouldDie = true;
            int threadsToKill = mNumThreadsAlive;
            DieTask dieTask = new DieTask();
            for (int i = 0; i < threadsToKill; i++) {
                mTasks.add(dieTask);
            }

            if (blockUntilFinished) {
                try {
                    mWaitingUntilAllFinishedLock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    @Override
    protected void finalize() throws Throwable {
        //make sure the threads are properly killed
        destroyAllConsumers(false);

        super.finalize();
    }

    public static abstract class Task {
        public abstract void doTask();
    }
{'fraction_non_alphanumeric': 0.07656458055925433, 'fraction_numerical': 0.003328894806924101, 'mean_word_length': 2.5284791544333527, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2565742', 'n_tokens_mistral': 1581, 'n_tokens_neox': 1516, 'n_words': 463}