001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.reef.wake.impl;
020
021import java.util.concurrent.ThreadFactory;
022import java.util.concurrent.atomic.AtomicInteger;
023
024/**
025 * A default thread factory implementation that names created threads.
026 */
027public final class DefaultThreadFactory implements ThreadFactory {
028
029  private static final AtomicInteger POOL_NUMBER = new AtomicInteger(1);
030
031  private final ThreadGroup group;
032  private final AtomicInteger threadNumber = new AtomicInteger(1);
033  private final String prefix;
034
035  private Thread.UncaughtExceptionHandler uncaughtExceptionHandler;
036
037  /**
038   * Constructs a default thread factory.
039   *
040   * @param prefix the name prefix of the created thread
041   */
042  public DefaultThreadFactory(final String prefix) {
043    this(prefix, null);
044  }
045
046  /**
047   * Constructs a default thread factory.
048   *
049   * @param prefix                   the name prefix of the created thread
050   * @param uncaughtExceptionHandler the uncaught exception handler of the created thread
051   */
052  public DefaultThreadFactory(final String prefix, final Thread.UncaughtExceptionHandler uncaughtExceptionHandler) {
053    final SecurityManager s = System.getSecurityManager();
054    this.group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
055    this.prefix = String.format("%s:pool-%02d", prefix, POOL_NUMBER.getAndIncrement());
056    this.uncaughtExceptionHandler = uncaughtExceptionHandler;
057  }
058
059  /**
060   * Sets a uncaught exception handler.
061   *
062   * @param uncaughtExceptionHandler the uncaught exception handler
063   */
064  public void setUncaughtExceptionHandler(final Thread.UncaughtExceptionHandler uncaughtExceptionHandler) {
065    this.uncaughtExceptionHandler = uncaughtExceptionHandler;
066  }
067
068  /**
069   * Creates a new thread.
070   *
071   * @param r the runnable
072   */
073  @Override
074  public Thread newThread(final Runnable r) {
075
076    final Thread t = new Thread(this.group, r,
077        String.format("%s:thread-%03d", this.prefix, this.threadNumber.getAndIncrement()), 0);
078
079    if (t.isDaemon()) {
080      t.setDaemon(false);
081    }
082
083    if (t.getPriority() != Thread.NORM_PRIORITY) {
084      t.setPriority(Thread.NORM_PRIORITY);
085    }
086
087    if (this.uncaughtExceptionHandler != null) {
088      t.setUncaughtExceptionHandler(this.uncaughtExceptionHandler);
089    }
090
091    return t;
092  }
093}