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 org.apache.log4j.Logger;
21
22 import com.google.common.util.concurrent.ThreadFactoryBuilder;
23
24 import java.util.concurrent.Callable;
25 import java.util.concurrent.ExecutorService;
26 import java.util.concurrent.Future;
27 import java.util.concurrent.ThreadFactory;
28
29 /**
30 * Utility class for thread related functions.
31 */
32 public class ThreadUtils {
33 /** Logger */
34 private static final Logger LOG = Logger.getLogger(ThreadUtils.class);
35
36 /**
37 * Utility class. Do not inherit or create objects.
38 */
39 private ThreadUtils() { }
40
41 /**
42 * Creates new thread factory with specified thread name format.
43 *
44 * @param nameFormat defines naming format for threads created by
45 * thread factory
46 * @param exceptionHandler handles uncaught exceptions in all threads
47 * produced created thread factory
48 * @return new thread factory with specified thread name format and
49 * exception handler.
50 */
51 public static ThreadFactory createThreadFactory(
52 String nameFormat,
53 Thread.UncaughtExceptionHandler exceptionHandler) {
54 ThreadFactoryBuilder builder = new ThreadFactoryBuilder().
55 setNameFormat(nameFormat).setDaemon(true);
56 if (exceptionHandler != null) {
57 builder.setUncaughtExceptionHandler(exceptionHandler);
58 }
59 return builder.build();
60 }
61
62 /**
63 * Creates new thread factory with specified thread name format.
64 *
65 * @param nameFormat defines naming format for threads created by
66 * thread factory
67 * @return new thread factory with specified thread name format
68 */
69 public static ThreadFactory createThreadFactory(String nameFormat) {
70 return createThreadFactory(nameFormat, null);
71 }
72
73 /**
74 * Submit a callable to executor service, ensuring any exceptions are
75 * caught with provided exception handler.
76 *
77 * When using submit(), UncaughtExceptionHandler which is set on ThreadFactory
78 * isn't used, so we need this utility.
79 *
80 * @param executorService Executor service to submit callable to
81 * @param callable Callable to submit
82 * @param uncaughtExceptionHandler Handler for uncaught exceptions in callable
83 * @param <T> Type of callable result
84 * @return Future for callable
85 */
86 public static <T> Future<T> submitToExecutor(
87 ExecutorService executorService,
88 Callable<T> callable,
89 Thread.UncaughtExceptionHandler uncaughtExceptionHandler) {
90 return executorService.submit(
91 new LogStacktraceCallable<>(callable, uncaughtExceptionHandler));
92 }
93
94 /**
95 * Start thread with specified name and runnable, and make it daemon
96 *
97 * @param threadName Name of the thread
98 * @param runnable Runnable to execute
99 * @return Thread
100 */
101 public static Thread startThread(Runnable runnable, String threadName) {
102 Thread thread = new Thread(runnable, threadName);
103 thread.setDaemon(true);
104 thread.start();
105 return thread;
106 }
107
108 /**
109 * Start thread with specified name, runnable and exception handler, and make
110 * it daemon
111 *
112 * @param runnable Runnable to execute
113 * @param threadName Name of the thread
114 * @param handler Exception handler
115 * @return Thread
116 */
117 public static Thread startThread(Runnable runnable, String threadName,
118 Thread.UncaughtExceptionHandler handler) {
119 Thread thread = new Thread(runnable, threadName);
120 thread.setUncaughtExceptionHandler(handler);
121 thread.setDaemon(true);
122 thread.start();
123 return thread;
124 }
125
126 /**
127 * Sleep for specified milliseconds, logging and ignoring interrupted
128 * exceptions
129 *
130 * @param millis How long to sleep for
131 * @return Whether the sleep was successful or the thread was interrupted
132 */
133 public static boolean trySleep(long millis) {
134 try {
135 Thread.sleep(millis);
136 return true;
137 } catch (InterruptedException e) {
138 if (LOG.isInfoEnabled()) {
139 LOG.info("Thread interrupted");
140 }
141 return false;
142 }
143 }
144 }