/*
 * Autopsy Forensic Browser
 *
 * Copyright 2017 Basis Technology Corp.
 * Contact: carrier <at> sleuthkit <dot> org
 *
 * 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 org.sleuthkit.autopsy.coreutils;

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

/*
 * Concurrent programming utilities.
 */
final public class ThreadUtils {

    private static final long DEFAULT_TIMEOUT = 5;
    private static final TimeUnit DEFAULT_TIMEOUT_UNITS = TimeUnit.SECONDS;

    /**
     * Shuts down a task executor service, unconditionally waiting until all
     * tasks are terminated.
     *
     * @param executor The executor.
     */
    public static void shutDownTaskExecutor(ExecutorService executor) {
        executor.shutdown();
        boolean tasksCompleted = false;
        while (!tasksCompleted) {
            try {
                tasksCompleted = executor.awaitTermination(DEFAULT_TIMEOUT, DEFAULT_TIMEOUT_UNITS);
            } catch (InterruptedException ignored) {
                /*
                 * Ignore interrupts. The policy implemented by this method is
                 * an unconditional wait.:
                 */
            }
        }
    }

    /**
     * A thread factory that allows for the creation of distinctly named task
     * threads by an ExecutorService constructed using an Executors factory
     * method. Each thread created using the factory will be named using the
     * thread name and a numerical suffix.
     */
    public static class NamedThreadFactory implements ThreadFactory {

        private final String threadName;

        /**
         * Contructs a thread factory that allows for the creation of distinctly
         * named task threads by an ExecutorService constructed using an
         * Executors factory method. Each thread created using the factory will
         * be named using the thread name and a numerical suffix.
         *
         * @param threadName The name of the threads.
         */
        public NamedThreadFactory(String threadName) {
            this.threadName = threadName;
        }

        @Override
        public Thread newThread(Runnable task) {
            return new Thread(task, threadName);
        }

    }

    private ThreadUtils() {
    }
}

@@ -766,51 +766,8 @@
 ce;%0A
-import java.util.concurrent.ThreadFactory;%0A
 impo
@@ -1715,1042 +1715,8 @@
  %7D%0A%0A
-    /**%0A     * A thread factory that allows for the creation of distinctly named task%0A     * threads by an ExecutorService constructed using an Executors factory%0A     * method. Each thread created using the factory will be named using the%0A     * thread name and a numerical suffix.%0A     */%0A    public static class NamedThreadFactory implements ThreadFactory %7B%0A%0A        private final String threadName;%0A%0A        /**%0A         * Contructs a thread factory that allows for the creation of distinctly%0A         * named task threads by an ExecutorService constructed using an%0A         * Executors factory method. Each thread created using the factory will%0A         * be named using the thread name and a numerical suffix.%0A         *%0A         * @param threadName The name of the threads.%0A         */%0A        public NamedThreadFactory(String threadName) %7B%0A            this.threadName = threadName;%0A        %7D%0A%0A        @Override%0A        public Thread newThread(Runnable task) %7B%0A            return new Thread(task, threadName);%0A        %7D%0A%0A    %7D%0A%0A
     

Revert "Add NamedThreadFactory to ThreadUtils"