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 POOL_NUMBER = 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(final String prefix) {
040    final SecurityManager s = System.getSecurityManager();
041    this.group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
042    this.prefix = prefix + "-pool-" + POOL_NUMBER.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(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 = prefix + "-pool-" + POOL_NUMBER.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(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    final Thread t = new Thread(group, r, prefix + threadNumber.getAndIncrement(), 0);
076    if (t.isDaemon()) {
077      t.setDaemon(false);
078    }
079    if (t.getPriority() != Thread.NORM_PRIORITY) {
080      t.setPriority(Thread.NORM_PRIORITY);
081    }
082    if (uncaughtExceptionHandler != null) {
083      t.setUncaughtExceptionHandler(uncaughtExceptionHandler);
084    }
085    return t;
086  }
087
088}