本文整理汇总了Java中java.util.concurrent.ThreadFactory.newThread方法的典型用法代码示例。如果您正苦于以下问题:Java ThreadFactory.newThread方法的具体用法?Java ThreadFactory.newThread怎么用?Java ThreadFactory.newThread使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.concurrent.ThreadFactory
的用法示例。
在下文中一共展示了ThreadFactory.newThread方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: ThreadPoolEventTarget
import java.util.concurrent.ThreadFactory; //导入方法依赖的package包/类
public ThreadPoolEventTarget(
final ThreadFactory wrappedFactory, final ThreadInitializer threadInitializer) {
int poolSize = 1;
BlockingQueue<Runnable> queue = new LinkedBlockingQueue<>();
executor = new ThreadPoolExecutor(poolSize, poolSize, 3, TimeUnit.SECONDS, queue,
new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread thread = wrappedFactory.newThread(r);
threadInitializer.setName(thread, "FirebaseDatabaseEventTarget");
threadInitializer.setDaemon(thread, true);
threadInitializer.setUncaughtExceptionHandler(thread, ThreadPoolEventTarget.this);
return thread;
}
});
}
示例2: testDeamonThreadFactory
import java.util.concurrent.ThreadFactory; //导入方法依赖的package包/类
private void testDeamonThreadFactory(final ThreadFactory deamons) throws InterruptedException {
/*
* Test that the runnable is actually executed
*/
final Runnable mock = mock(Runnable.class);
final CountDownLatch latch = new CountDownLatch(1);
doAnswer(new Answer<Void>() {
@Override
public Void answer(final InvocationOnMock invocation) throws Throwable {
latch.countDown();
return null;
}
}).when(mock).run();
final Thread t = deamons.newThread(mock);
assertThat(t.isDaemon(), is(true));
t.start();
assertTrue(latch.await(5, TimeUnit.SECONDS));
}
示例3: newDaemonThreadFactory
import java.util.concurrent.ThreadFactory; //导入方法依赖的package包/类
/**
* Get a named {@link ThreadFactory} that just builds daemon threads.
* @param prefix name prefix for all threads created from the factory
* @return a thread factory that creates named, daemon threads with
* the supplied exception handler and normal priority
*/
private static ThreadFactory newDaemonThreadFactory(final String prefix) {
final ThreadFactory namedFactory = getNamedThreadFactory(prefix);
return new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread t = namedFactory.newThread(r);
if (!t.isDaemon()) {
t.setDaemon(true);
}
if (t.getPriority() != Thread.NORM_PRIORITY) {
t.setPriority(Thread.NORM_PRIORITY);
}
return t;
}
};
}
示例4: IdleAsyncConnectionEvictor
import java.util.concurrent.ThreadFactory; //导入方法依赖的package包/类
public IdleAsyncConnectionEvictor(
final NHttpClientConnectionManager connectionManager,
final ThreadFactory threadFactory,
final long sleepTime, final TimeUnit sleepTimeUnit,
final long maxIdleTime, final TimeUnit maxIdleTimeUnit) {
ThreadFactory threadFactory1 = threadFactory != null ? threadFactory : new DefaultThreadFactory();
this.sleepTimeMs = sleepTimeUnit != null ? sleepTimeUnit.toMillis(sleepTime) : sleepTime;
this.maxIdleTimeMs = maxIdleTimeUnit != null ? maxIdleTimeUnit.toMillis(maxIdleTime) : maxIdleTime;
this.thread = threadFactory1.newThread(new Runnable() {
@Override
public void run() {
try {
while (!Thread.currentThread().isInterrupted()) {
Thread.sleep(sleepTimeMs);
connectionManager.closeExpiredConnections();
if (maxIdleTimeMs > 0) {
connectionManager.closeIdleConnections(maxIdleTimeMs, TimeUnit.MILLISECONDS);
}
}
} catch (final Exception ex) {
log.warn("Evictor exception", ex);
}
}
});
}
示例5: BulkProcessor
import java.util.concurrent.ThreadFactory; //导入方法依赖的package包/类
public BulkProcessor(
Time time,
BulkClient<R, B> bulkClient,
int maxBufferedRecords,
int maxInFlightRequests,
int batchSize,
long lingerMs,
int maxRetries,
long retryBackoffMs
) {
this.time = time;
this.bulkClient = bulkClient;
this.maxBufferedRecords = maxBufferedRecords;
this.batchSize = batchSize;
this.lingerMs = lingerMs;
this.maxRetries = maxRetries;
this.retryBackoffMs = retryBackoffMs;
unsentRecords = new ArrayDeque<>(maxBufferedRecords);
final ThreadFactory threadFactory = makeThreadFactory();
farmer = threadFactory.newThread(farmerTask());
executor = Executors.newFixedThreadPool(maxInFlightRequests, threadFactory);
}
示例6: start
import java.util.concurrent.ThreadFactory; //导入方法依赖的package包/类
/**
* Starts the Cleaner implementation.
* Ensure this is the CleanerImpl for the Cleaner.
* When started waits for Cleanables to be queued.
* @param cleaner the cleaner
* @param threadFactory the thread factory
*/
public void start(Cleaner cleaner, ThreadFactory threadFactory) {
if (getCleanerImpl(cleaner) != this) {
throw new AssertionError("wrong cleaner");
}
// schedule a nop cleaning action for the cleaner, so the associated thread
// will continue to run at least until the cleaner is reclaimable.
new CleanerCleanable(cleaner);
if (threadFactory == null) {
threadFactory = CleanerImpl.InnocuousThreadFactory.factory();
}
// now that there's at least one cleaning action, for the cleaner,
// we can start the associated thread, which runs until
// all cleaning actions have been run.
Thread thread = threadFactory.newThread(this);
thread.setDaemon(true);
thread.start();
}
示例7: build
import java.util.concurrent.ThreadFactory; //导入方法依赖的package包/类
private static ThreadFactory build(ThreadFactoryBuilder builder) {
final String nameFormat = builder.nameFormat;
final Boolean daemon = builder.daemon;
final Integer priority = builder.priority;
final Thread.UncaughtExceptionHandler uncaughtExceptionHandler = builder.uncaughtExceptionHandler;
final ThreadFactory backingThreadFactory =
(builder.backingThreadFactory != null)
? builder.backingThreadFactory
: Executors.defaultThreadFactory();
final AtomicLong count = (nameFormat != null) ? new AtomicLong(0) : null;
return new ThreadFactory() {
@Override
public Thread newThread(Runnable runnable) {
Thread thread = backingThreadFactory.newThread(runnable);
if (nameFormat != null) {
thread.setName(format(nameFormat, count.getAndIncrement()));
}
if (daemon != null) {
thread.setDaemon(daemon);
}
if (priority != null) {
thread.setPriority(priority);
}
if (uncaughtExceptionHandler != null) {
thread.setUncaughtExceptionHandler(uncaughtExceptionHandler);
}
return thread;
}
};
}
示例8: start
import java.util.concurrent.ThreadFactory; //导入方法依赖的package包/类
synchronized void start(UncaughtExceptionHandler eh) {
ThreadFactory flusherThreadFactory = Threads.newDaemonThreadFactory(
server.getServerName().toShortString() + "-MemStoreFlusher", eh);
for (int i = 0; i < flushHandlers.length; i++) {
flushHandlers[i] = new FlushHandler("MemStoreFlusher." + i);
flusherThreadFactory.newThread(flushHandlers[i]);
flushHandlers[i].start();
}
}
示例9: testPriority_custom
import java.util.concurrent.ThreadFactory; //导入方法依赖的package包/类
public void testPriority_custom() {
for (int i = Thread.MIN_PRIORITY; i <= Thread.MAX_PRIORITY; i++) {
ThreadFactory factory = builder.setPriority(i).build();
Thread thread = factory.newThread(monitoredRunnable);
assertEquals(i, thread.getPriority());
}
}
示例10: namedThreadFactory
import java.util.concurrent.ThreadFactory; //导入方法依赖的package包/类
@Provides @Singleton
NamedThreadFactory namedThreadFactory(ThreadFactory factory) {
return (name, code) -> {
final Thread thread = factory.newThread(code);
thread.setName(name);
return thread;
};
}
示例11: groupedThreads
import java.util.concurrent.ThreadFactory; //导入方法依赖的package包/类
@Test
public void groupedThreads() {
ThreadFactory f = Tools.groupedThreads("foo/bar-me", "foo-%d");
Thread t = f.newThread(() -> TestTools.print("yo"));
assertTrue("wrong pattern", t.getName().startsWith("foo-bar-me-foo-"));
assertTrue("wrong group", t.getThreadGroup().getName().equals("foo/bar-me"));
}
示例12: exceptionHandler
import java.util.concurrent.ThreadFactory; //导入方法依赖的package包/类
@Test
public void exceptionHandler() throws InterruptedException {
ThreadFactory f = Tools.namedThreads("foo");
Thread t = f.newThread(() -> {
throw new IllegalStateException("BOOM!");
});
assertNotNull("thread should have exception handler", t.getUncaughtExceptionHandler());
t.start();
assertAfter(100, () -> assertEquals("incorrect thread state", Thread.State.TERMINATED, t.getState()));
}
示例13: test
import java.util.concurrent.ThreadFactory; //导入方法依赖的package包/类
void test(final Class<?> exceptionClass,
final ThreadFactory failingThreadFactory)
throws Throwable {
ThreadFactory flakyThreadFactory = new ThreadFactory() {
int seq = 0;
public Thread newThread(Runnable r) {
if (seq++ < 4)
return new Thread(r);
else
return failingThreadFactory.newThread(r);
}};
ThreadPoolExecutor pool =
new ThreadPoolExecutor(10, 10,
0L, TimeUnit.SECONDS,
new LinkedBlockingQueue(),
flakyThreadFactory);
try {
for (int i = 0; i < 8; i++)
pool.submit(new Runnable() { public void run() {} });
check(exceptionClass == null);
} catch (Throwable t) {
/* t.printStackTrace(); */
check(exceptionClass.isInstance(t));
}
pool.shutdown();
check(pool.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
}
示例14: build
import java.util.concurrent.ThreadFactory; //导入方法依赖的package包/类
private static ThreadFactory build(ThreadFactoryBuilder builder) {
final String nameFormat = builder.nameFormat;
final Boolean daemon = builder.daemon;
final Integer priority = builder.priority;
final UncaughtExceptionHandler uncaughtExceptionHandler = builder.uncaughtExceptionHandler;
final ThreadFactory backingThreadFactory =
(builder.backingThreadFactory != null)
? builder.backingThreadFactory
: Executors.defaultThreadFactory();
final AtomicLong count = (nameFormat != null) ? new AtomicLong(0) : null;
return new ThreadFactory() {
@Override
public Thread newThread(Runnable runnable) {
Thread thread = backingThreadFactory.newThread(runnable);
if (nameFormat != null) {
thread.setName(format(nameFormat, count.getAndIncrement()));
}
if (daemon != null) {
thread.setDaemon(daemon);
}
if (priority != null) {
thread.setPriority(priority);
}
if (uncaughtExceptionHandler != null) {
thread.setUncaughtExceptionHandler(uncaughtExceptionHandler);
}
return thread;
}
};
}
示例15: testThreadFactoryBuilder_defaults
import java.util.concurrent.ThreadFactory; //导入方法依赖的package包/类
public void testThreadFactoryBuilder_defaults() throws InterruptedException {
ThreadFactory threadFactory = builder.build();
Thread thread = threadFactory.newThread(monitoredRunnable);
checkThreadPoolName(thread, 1);
Thread defaultThread =
Executors.defaultThreadFactory().newThread(monitoredRunnable);
assertEquals(defaultThread.isDaemon(), thread.isDaemon());
assertEquals(defaultThread.getPriority(), thread.getPriority());
assertSame(defaultThread.getThreadGroup(), thread.getThreadGroup());
assertSame(defaultThread.getUncaughtExceptionHandler(),
thread.getUncaughtExceptionHandler());
assertFalse(completed);
thread.start();
thread.join();
assertTrue(completed);
// Creating a new thread from the same ThreadFactory will have the same
// pool ID but a thread ID of 2.
Thread thread2 = threadFactory.newThread(monitoredRunnable);
checkThreadPoolName(thread2, 2);
assertEquals(
thread.getName().substring(0, thread.getName().lastIndexOf('-')),
thread2.getName().substring(0, thread.getName().lastIndexOf('-')));
// Building again should give us a different pool ID.
ThreadFactory threadFactory2 = builder.build();
Thread thread3 = threadFactory2.newThread(monitoredRunnable);
checkThreadPoolName(thread3, 1);
assertThat(
thread2.getName().substring(0, thread.getName().lastIndexOf('-')))
.isNotEqualTo(
thread3.getName().substring(0, thread.getName().lastIndexOf('-')));
}