Java tutorial
// Copyright (C) 2015 chief8192@gmail.com // // 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.chiefly.nautilus.concurrent; import java.util.Collection; import java.util.Set; import com.google.common.collect.Sets; /** Utility methods for {@link Thread}s. */ public class Threads { /** * Calls {@code start()} on all the {@link Thread}s and returns. * * @param threads The {@link Thread}s to {@code start()}. */ public static <T extends Thread> void runAll(final Collection<T> threads) { for (final T thread : threads) { thread.start(); } } /** * Calls {@code start()} on all the {@link Thread}s and returns. * * @param threads The {@link Thread}s to {@code start()}. */ public static <T extends Thread> void runAll(final T... threads) { final Set<T> set = Sets.newHashSet(threads); runAll(set); } /** * Calls {@code start()} and {@code join()} on all the {@link Thread}s. * * @param threads The {@link Thread}s to {@code start()}. */ public static <T extends Thread> void runAllAndWait(final Collection<T> threads) throws InterruptedException { for (final T thread : threads) { thread.start(); } for (final T thread : threads) { thread.join(); } } /** * Calls {@code start()} and {@code join()} on all the {@link Thread}s. * * @param threads The {@link Thread}s to {@code start()}. */ public static <T extends Thread> void runAllAndWait(final T... threads) throws InterruptedException { final Set<T> set = Sets.newHashSet(threads); runAllAndWait(set); } }