Search in sources :

Example 6 with ThreadFactory

use of java.util.concurrent.ThreadFactory in project torodb by torodb.

the class StampedeBootstrap method createStampedeService.

public static Service createStampedeService(BootstrapModule bootstrapModule) {
    Injector bootstrapInjector = Guice.createInjector(bootstrapModule);
    ThreadFactory threadFactory = bootstrapInjector.getInstance(ThreadFactory.class);
    return new StampedeService(threadFactory, bootstrapInjector);
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) Injector(com.google.inject.Injector)

Example 7 with ThreadFactory

use of java.util.concurrent.ThreadFactory in project jstorm by alibaba.

the class NettyContext method prepare.

/**
     * initialization per Storm configuration
     */
@SuppressWarnings("rawtypes")
public void prepare(Map storm_conf) {
    this.storm_conf = storm_conf;
    int maxWorkers = Utils.getInt(storm_conf.get(Config.STORM_MESSAGING_NETTY_CLIENT_WORKER_THREADS));
    ThreadFactory bossFactory = new NettyRenameThreadFactory(MetricDef.NETTY_CLI + "boss");
    ThreadFactory workerFactory = new NettyRenameThreadFactory(MetricDef.NETTY_CLI + "worker");
    if (maxWorkers > 0) {
        clientChannelFactory = new NioClientSocketChannelFactory(Executors.newCachedThreadPool(bossFactory), Executors.newCachedThreadPool(workerFactory), maxWorkers);
    } else {
        clientChannelFactory = new NioClientSocketChannelFactory(Executors.newCachedThreadPool(bossFactory), Executors.newCachedThreadPool(workerFactory));
    }
    reconnector = new ReconnectRunnable();
    new AsyncLoopThread(reconnector, true, Thread.MIN_PRIORITY, true);
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) NioClientSocketChannelFactory(org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory) AsyncLoopThread(com.alibaba.jstorm.callback.AsyncLoopThread)

Example 8 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 9 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 10 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)250 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)47 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)46 ExecutorService (java.util.concurrent.ExecutorService)45 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)35 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)21 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)19 Test (org.junit.Test)17 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)16 Future (java.util.concurrent.Future)15 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)13 ArrayList (java.util.ArrayList)13 LoggingThreadGroup (org.apache.geode.internal.logging.LoggingThreadGroup)12 IOException (java.io.IOException)10 AtomicLong (java.util.concurrent.atomic.AtomicLong)10 ExecutionException (java.util.concurrent.ExecutionException)9 Executor (java.util.concurrent.Executor)9 ChannelFuture (io.netty.channel.ChannelFuture)8 DefaultThreadFactory (io.netty.util.concurrent.DefaultThreadFactory)8 SynchronousQueue (java.util.concurrent.SynchronousQueue)7