a235ff497e0b638c2a1ec264b5aac7c209116878
[giraph.git] / giraph-core / src / main / java / org / apache / giraph / utils / ThreadUtils.java
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.ThreadFactory;
23
24 /**
25 * Utility class for thread related functions.
26 */
27 public class ThreadUtils {
28
29 /**
30 * Utility class. Do not inherit or create objects.
31 */
32 private ThreadUtils() { }
33
34 /**
35 * Creates new thread factory with specified thread name format.
36 *
37 * @param nameFormat defines naming format for threads created by
38 * thread factory
39 * @param exceptionHandler handles uncaught exceptions in all threads
40 * produced created thread factory
41 * @return new thread factory with specified thread name format and
42 * exception handler.
43 */
44 public static ThreadFactory createThreadFactory(
45 String nameFormat,
46 Thread.UncaughtExceptionHandler exceptionHandler) {
47 ThreadFactoryBuilder builder = new ThreadFactoryBuilder().
48 setNameFormat(nameFormat);
49 if (exceptionHandler != null) {
50 builder.setUncaughtExceptionHandler(exceptionHandler);
51 }
52 return builder.build();
53 }
54 }