Search in sources :

Example 56 with ThreadFactory

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();
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) MoreExecutors.newDirectExecutorService(com.google.common.util.concurrent.MoreExecutors.newDirectExecutorService) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor)

Example 57 with ThreadFactory

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);
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor)

Example 58 with ThreadFactory

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;
        }
    };
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) AtomicLong(java.util.concurrent.atomic.AtomicLong) UncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler)

Example 59 with ThreadFactory

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());
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory)

Example 60 with ThreadFactory

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('-')));
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory)

Aggregations

ThreadFactory (java.util.concurrent.ThreadFactory)214 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)40 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)38 ExecutorService (java.util.concurrent.ExecutorService)35 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)28 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)18 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)18 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)14 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)13 LoggingThreadGroup (org.apache.geode.internal.logging.LoggingThreadGroup)12 Future (java.util.concurrent.Future)11 Test (org.junit.Test)11 AtomicLong (java.util.concurrent.atomic.AtomicLong)10 IOException (java.io.IOException)9 ArrayList (java.util.ArrayList)9 ChannelFuture (io.netty.channel.ChannelFuture)8 DefaultThreadFactory (io.netty.util.concurrent.DefaultThreadFactory)8 ExecutionException (java.util.concurrent.ExecutionException)7 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)6 UdtChannel (io.netty.channel.udt.UdtChannel)6