Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3cf533f258a9125351f953f10f14cb1176df50c7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*******************************************************************************
 * Copyright (c) 2011 Wind River Systems, Inc. and others. All rights reserved.
 * This program and the accompanying materials are made available under the terms
 * of the Eclipse Public License v1.0 which accompanies this distribution, and is
 * available at http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.te.runtime.concurrent.factories;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

import org.eclipse.core.runtime.Assert;

/**
 * A thread factory implementation creating a single thread only.
 */
public class SingleThreadThreadFactory implements ThreadFactory {
	private final ThreadGroup threadGroup;
	private final String threadName;
	private Thread thread;

	private final AtomicInteger threadNumber = new AtomicInteger(1);

	/**
	 * Constructor.
	 *
	 * @param namePrefix
	 *            The name prefix to name the created threads. Must not be
	 *            <code>null</code>.
	 */
	public SingleThreadThreadFactory(String namePrefix) {
		Assert.isNotNull(namePrefix);

		// Determine the thread group. Use the security manager if available.
		this.threadGroup = (System.getSecurityManager() != null) ? System.getSecurityManager().getThreadGroup() : Thread.currentThread().getThreadGroup();
		// Set the thread name prefix
		this.threadName = ("".equals(namePrefix.trim()) ? "Executor" : namePrefix) + " - " + threadNumber.getAndIncrement(); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see java.util.concurrent.ThreadFactory#newThread(java.lang.Runnable)
	 */
	@Override
	public Thread newThread(Runnable r) {
		// The thread can be created on once. If called a second time,
		// this factory cannot create any additional threads.
		if (thread != null) return null;

		// Create the thread with the desired name and the current thread number
		thread = new Thread(threadGroup, r, threadName);
		thread.setDaemon(false);
		thread.setPriority(Thread.NORM_PRIORITY);

		// Return the thread
		return thread;
	}

	/**
	 * Returns the single created thread instance or <code>null</code> if
	 * {@link #newThread(Runnable)} have not been called yet.
	 *
	 * @return The single created thread instance or <code>null</code>.
	 */
	public Thread getThread() {
		return thread;
	}
}

Back to the top