Java tutorial
/* * (c) 2005 David B. Bracewell * * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you 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.davidbracewell.concurrent; import com.davidbracewell.logging.Logger; import com.google.common.base.Function; import com.google.common.base.Preconditions; import com.google.common.collect.Lists; import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; import java.util.logging.Level; /** * The type Threads. * * @author David B. Bracewell */ public class Threads { private static final Logger log = Logger.getLogger(Threads.class); /** * <p> Sleeps the thread suppressing any errors. </p> * * @param milliseconds The amount of time in milliseconds to sleep */ public static void sleep(long milliseconds) { Preconditions.checkArgument(milliseconds >= 0); try { if (log.isLoggable(Level.FINEST)) { log.finest("Thread {0} is going to sleep for {1} milliseconds.", Thread.currentThread().getName(), milliseconds); } Thread.sleep(milliseconds); } catch (InterruptedException e) { log.warn(e); } } /** * <p> Sleeps the thread suppressing any errors for a given time unit. </p> * * @param time The amount of time to sleep * @param timeUnit The TimeUnit that the time is in */ public static void sleep(long time, TimeUnit timeUnit) { Preconditions.checkArgument(time >= 0); sleep(timeUnit.toMillis(time)); } /** * Executes a number of processes using a given number of threads * * @param processes The processes to run * @param numberOfThreads The number of threads to use * @return True if the execution was successful, false otherwise. */ public static boolean execute(Iterable<? extends Runnable> processes, int numberOfThreads) { Preconditions.checkNotNull(processes); Preconditions.checkArgument(numberOfThreads > 0); final ExecutorService service = Executors.newFixedThreadPool(numberOfThreads); for (Runnable runnable : processes) { service.execute(runnable); } try { service.shutdown(); service.awaitTermination(Integer.MAX_VALUE, TimeUnit.SECONDS); } catch (InterruptedException e) { log.warn(e); return false; } return true; } /** * Execute boolean. * * @param processes the processes * @return the boolean */ public static boolean execute(Iterable<? extends Runnable> processes) { return execute(processes, Runtime.getRuntime().availableProcessors()); } /** * Execute list. * * @param source the source * @param processor the processor * @return the list */ public static <IN, OUT> List<OUT> execute(Iterable<IN> source, final Function<IN, OUT> processor) { return execute(source, processor, Runtime.getRuntime().availableProcessors(), Runtime.getRuntime().availableProcessors() * 10); } /** * Execute list. * * @param source the source * @param processor the processor * @param numberOfThreads the number of threads * @param queueSize the queue size * @return the list */ public static <IN, OUT> List<OUT> execute(Iterable<IN> source, final Function<IN, OUT> processor, int numberOfThreads, int queueSize) { Preconditions.checkNotNull(source); Preconditions.checkNotNull(processor); Preconditions.checkArgument(numberOfThreads > 0, "Must specified at least 1 thread."); Preconditions.checkArgument(queueSize > 0, "Must specified a queue size of at least 1."); final List<OUT> out = Lists.newCopyOnWriteArrayList(); BlockingThreadPoolExecutor executor = new BlockingThreadPoolExecutor(numberOfThreads, numberOfThreads, queueSize); for (final IN item : source) { executor.execute(new Runnable() { @Override public void run() { out.add(processor.apply(item)); } }); } executor.shutdown(); try { executor.awaitTermination(Integer.MAX_VALUE, TimeUnit.SECONDS); } catch (InterruptedException e) { log.warn(e); } return out; } }// END OF CLASS Threads