This project has retired. For details please refer to its Attic page.
Source code
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  private static final AtomicInteger poolNumber = new AtomicInteger(1);
029  private final ThreadGroup group;
030  private final AtomicInteger threadNumber = new AtomicInteger(1);
031  private final String prefix;
032  private Thread.UncaughtExceptionHandler uncaughtExceptionHandler;
033
034  /**
035   * Constructs a default thread factory
036   *
037   * @param prefix the name prefix of the created thread
038   */
039  public DefaultThreadFactory(String prefix) {
040    SecurityManager s = System.getSecurityManager();
041    this.group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
042    this.prefix = prefix + "-pool-" + poolNumber.getAndIncrement() + "-thread-";
043    this.uncaughtExceptionHandler = 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(String prefix, Thread.UncaughtExceptionHandler uncaughtExceptionHandler) {
053    SecurityManager s = System.getSecurityManager();
054    this.group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
055    this.prefix = prefix + "-pool-" + poolNumber.getAndIncrement() + "-thread-";
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(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(Runnable r) {
075    Thread t = new Thread(group, r, prefix + threadNumber.getAndIncrement(), 0);
076    if (t.isDaemon())
077      t.setDaemon(false);
078    if (t.getPriority() != Thread.NORM_PRIORITY)
079      t.setPriority(Thread.NORM_PRIORITY);
080    if (uncaughtExceptionHandler != null)
081      t.setUncaughtExceptionHandler(uncaughtExceptionHandler);
082    return t;
083  }
084
085}