use of java.util.concurrent.ThreadFactory in project guava by google.
the class MoreExecutorsTest method testGetExitingExcutorService_shutdownHookRegistered.
public void testGetExitingExcutorService_shutdownHookRegistered() throws InterruptedException {
TestApplication application = new TestApplication();
ThreadPoolExecutor executor = mock(ThreadPoolExecutor.class);
ThreadFactory threadFactory = mock(ThreadFactory.class);
when(executor.getThreadFactory()).thenReturn(threadFactory);
ExecutorService unused = application.getExitingExecutorService(executor);
application.shutdown();
verify(executor).shutdown();
}
use of java.util.concurrent.ThreadFactory in project guava by google.
the class MoreExecutorsTest method testGetExitingScheduledExcutorService_executorDelegatesToOriginal.
public void testGetExitingScheduledExcutorService_executorDelegatesToOriginal() {
TestApplication application = new TestApplication();
ScheduledThreadPoolExecutor executor = mock(ScheduledThreadPoolExecutor.class);
ThreadFactory threadFactory = mock(ThreadFactory.class);
when(executor.getThreadFactory()).thenReturn(threadFactory);
application.getExitingScheduledExecutorService(executor).execute(EMPTY_RUNNABLE);
verify(executor).execute(EMPTY_RUNNABLE);
}
use of java.util.concurrent.ThreadFactory in project guava by hceylan.
the class ThreadFactoryBuilder method build.
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(String.format(nameFormat, count.getAndIncrement()));
}
if (daemon != null) {
thread.setDaemon(daemon);
}
if (priority != null) {
thread.setPriority(priority);
}
if (uncaughtExceptionHandler != null) {
thread.setUncaughtExceptionHandler(uncaughtExceptionHandler);
}
return thread;
}
};
}
use of java.util.concurrent.ThreadFactory in project guava by hceylan.
the class ThreadFactoryBuilderTest method testBuildMutate.
public void testBuildMutate() {
ThreadFactory factory1 = builder.setPriority(1).build();
assertEquals(1, factory1.newThread(monitoredRunnable).getPriority());
// change the state of the builder
builder.setPriority(2);
assertEquals(1, factory1.newThread(monitoredRunnable).getPriority());
}
use of java.util.concurrent.ThreadFactory in project guava by hceylan.
the class ThreadFactoryBuilderTest method testThreadFactoryBuilder_defaults.
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);
ASSERT.that(thread2.getName().substring(0, thread.getName().lastIndexOf('-'))).isNotEqualTo(thread3.getName().substring(0, thread.getName().lastIndexOf('-')));
}
Aggregations