View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.giraph.utils;
19  
20  import com.google.common.util.concurrent.ThreadFactoryBuilder;
21  
22  import java.util.concurrent.Callable;
23  import java.util.concurrent.ExecutorService;
24  import java.util.concurrent.Future;
25  import java.util.concurrent.ThreadFactory;
26  
27  /**
28   * Utility class for thread related functions.
29   */
30  public class ThreadUtils {
31  
32    /**
33     * Utility class. Do not inherit or create objects.
34     */
35    private ThreadUtils() { }
36  
37    /**
38     * Creates new thread factory with specified thread name format.
39     *
40     * @param nameFormat defines naming format for threads created by
41     *                   thread factory
42     * @param exceptionHandler handles uncaught exceptions in all threads
43     *                         produced created thread factory
44     * @return new thread factory with specified thread name format and
45     * exception handler.
46     */
47    public static ThreadFactory createThreadFactory(
48        String nameFormat,
49        Thread.UncaughtExceptionHandler exceptionHandler) {
50      ThreadFactoryBuilder builder = new ThreadFactoryBuilder().
51          setNameFormat(nameFormat).setDaemon(true);
52      if (exceptionHandler != null) {
53        builder.setUncaughtExceptionHandler(exceptionHandler);
54      }
55      return builder.build();
56    }
57  
58    /**
59     * Creates new thread factory with specified thread name format.
60     *
61     * @param nameFormat defines naming format for threads created by
62     *                   thread factory
63     * @return new thread factory with specified thread name format
64     */
65    public static ThreadFactory createThreadFactory(String nameFormat) {
66      return createThreadFactory(nameFormat, null);
67    }
68  
69    /**
70     * Submit a callable to executor service, ensuring any exceptions are
71     * caught with provided exception handler.
72     *
73     * When using submit(), UncaughtExceptionHandler which is set on ThreadFactory
74     * isn't used, so we need this utility.
75     *
76     * @param executorService Executor service to submit callable to
77     * @param callable Callable to submit
78     * @param uncaughtExceptionHandler Handler for uncaught exceptions in callable
79     * @param <T> Type of callable result
80     * @return Future for callable
81     */
82    public static <T> Future<T> submitToExecutor(
83        ExecutorService executorService,
84        Callable<T> callable,
85        Thread.UncaughtExceptionHandler uncaughtExceptionHandler) {
86      return executorService.submit(
87          new LogStacktraceCallable<>(callable, uncaughtExceptionHandler));
88    }
89  }