Example usage for com.google.common.util.concurrent ThreadFactoryBuilder ThreadFactoryBuilder

List of usage examples for com.google.common.util.concurrent ThreadFactoryBuilder ThreadFactoryBuilder

Introduction

In this page you can find the example usage for com.google.common.util.concurrent ThreadFactoryBuilder ThreadFactoryBuilder.

Prototype

public ThreadFactoryBuilder() 

Source Link

Document

Creates a new ThreadFactory builder.

Usage

From source file:org.pantsbuild.tools.junit.ConcurrentRunnerScheduler.java

/**
 * A concurrent scheduler to run junit tests in parallel if possible, followed by tests that can
 * only be run in serial./*from  ww  w .jav  a  2 s  .  com*/
 *
 * Test classes annotated with {@link TestSerial} will be run in serial.
 * Test classes annotated with {@link TestParallel} will be run in parallel.
 * Test classes without neither annotation will be run in parallel if defaultParallel is set.
 *
 * Call {@link org.junit.runners.ParentRunner#setScheduler} to use this scheduler.
 *
 * @param defaultParallel  whether to unannotated classes in parallel
 * @param numThreads       number of parallel threads to use, must be positive.
 */
public ConcurrentRunnerScheduler(boolean defaultParallel, int numThreads) {
    this.defaultParallel = defaultParallel;
    ThreadFactory threadFactory = new ThreadFactoryBuilder().setDaemon(true)
            .setNameFormat("concurrent-junit-runner-%d").build();
    completionService = new ExecutorCompletionService<Void>(
            Executors.newFixedThreadPool(numThreads, threadFactory));
    concurrentTasks = new LinkedList<Future<Void>>();
    serialTasks = new LinkedList<Runnable>();
}

From source file:net.myrrix.online.candidate.ExampleCandidateFilter.java

public ExampleCandidateFilter(FastByIDMap<float[]> Y) {
    this.Y = Y;// w w w  . j a v  a  2 s  . co m
    lock = new ReentrantReadWriteLock();
    int reloadMinutes = 15;
    ScheduledExecutorService executor = Executors.newScheduledThreadPool(1,
            new ThreadFactoryBuilder().setDaemon(true).build());
    executor.scheduleWithFixedDelay(new Runnable() {
        @Override
        public void run() {
            doLoad();
        }
    }, reloadMinutes, reloadMinutes, TimeUnit.MINUTES);
    doLoad();
}

From source file:org.apache.bookkeeper.proto.NioServerSocketChannelManager.java

@Override
public Channel start(ServerConfiguration conf, ChannelPipelineFactory bookiePipelineFactory)
        throws IOException {
    BookieSocketAddress bookieAddress = Bookie.getBookieAddress(conf);
    ThreadFactoryBuilder tfb = new ThreadFactoryBuilder();
    String base = "bookie-" + conf.getBookiePort() + "-netty";
    this.channelFactory = new NioServerSocketChannelFactory(
            Executors.newCachedThreadPool(tfb.setNameFormat(base + "-boss-%d").build()),
            Executors.newCachedThreadPool(tfb.setNameFormat(base + "-worker-%d").build()));

    ServerBootstrap bootstrap = new ServerBootstrap(channelFactory);
    bootstrap.setPipelineFactory(bookiePipelineFactory);
    bootstrap.setOption("child.tcpNoDelay", conf.getServerTcpNoDelay());
    bootstrap.setOption("child.soLinger", 2);

    InetSocketAddress bindAddress;
    if (conf.getListeningInterface() == null) {
        // listen on all interfaces
        bindAddress = new InetSocketAddress(conf.getBookiePort());
    } else {/*ww  w.  j av a2s.c  o m*/
        bindAddress = bookieAddress.getSocketAddress();
    }

    Channel listen = bootstrap.bind(bindAddress);
    return listen;
}

From source file:com.google.gerrit.server.project.ProjectCacheClock.java

public ProjectCacheClock(long checkFrequencyMillis) {
    if (checkFrequencyMillis == Long.MAX_VALUE) {
        // Start with generation 1 (to avoid magic 0 below).
        // Do not begin background thread, disabling the clock.
        generation = 1;/*from w w  w .  ja  v  a 2  s .c  o m*/
    } else if (10 < checkFrequencyMillis) {
        // Start with generation 1 (to avoid magic 0 below).
        generation = 1;
        ScheduledExecutorService executor = Executors.newScheduledThreadPool(1,
                new ThreadFactoryBuilder().setNameFormat("ProjectCacheClock-%d").setDaemon(true)
                        .setPriority(Thread.MIN_PRIORITY).build());
        executor.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                // This is not exactly thread-safe, but is OK for our use.
                // The only thread that writes the volatile is this task.
                generation = generation + 1;
            }
        }, checkFrequencyMillis, checkFrequencyMillis, TimeUnit.MILLISECONDS);
    } else {
        // Magic generation 0 triggers ProjectState to always
        // check on each needsRefresh() request we make to it.
        generation = 0;
    }
}

From source file:org.eclipse.che.mail.MailSender.java

@Inject
public MailSender(MailSessionProvider mailSessionProvider) {
    this.mailSessionProvider = mailSessionProvider;
    this.executor = newFixedThreadPool(2 * Runtime.getRuntime().availableProcessors(),
            new ThreadFactoryBuilder().setNameFormat("MailNotificationsPool-%d").setDaemon(false)
                    .setUncaughtExceptionHandler(LoggingUncaughtExceptionHandler.getInstance()).build());
}

From source file:com.torodb.core.guice.ExecutorServicesModule.java

@Override
protected void configure() {

    bind(Integer.class).annotatedWith(ParallelLevel.class)
            .toInstance(Runtime.getRuntime().availableProcessors());

    ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("torodb-%d").build();

    bind(ThreadFactory.class).toInstance(threadFactory);

    bind(ThreadFactory.class).annotatedWith(TorodbIdleService.class).toInstance(threadFactory);

    bind(ThreadFactory.class).annotatedWith(TorodbRunnableService.class).toInstance(threadFactory);

    bind(ForkJoinWorkerThreadFactory.class).toInstance(ForkJoinPool.defaultForkJoinWorkerThreadFactory);

    bind(DefaultConcurrentToolsFactory.BlockerThreadFactoryFunction.class)
            .toInstance(new CustomBlockerThreadFactoryFunction());

    bind(DefaultConcurrentToolsFactory.ForkJoinThreadFactoryFunction.class)
            .toInstance(new CustomForkJoinThreadFactoryFunction());
}

From source file:org.apache.servicecomb.foundation.metrics.MetricsBootstrap.java

public void start(CompositeRegistry globalRegistry, EventBus eventBus) {
    this.globalRegistry = globalRegistry;
    this.eventBus = eventBus;
    this.executorService = Executors.newScheduledThreadPool(1,
            new ThreadFactoryBuilder().setNameFormat("spectator-poller-%d").build());

    loadMetricsInitializers();//  w w w.j a v a  2s.c  om
    startPoll();
}

From source file:org.artifactory.schedule.ArtifactorySingleThreadExecutor.java

public ArtifactorySingleThreadExecutor() {
    ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("art-fixed-%s").build();
    executor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(),
            threadFactory);//from   w ww.  ja  va  2s .com
    storageContext = StorageContextHelper.get();
}

From source file:org.eclipse.che.api.workspace.server.WorkspaceSharedPool.java

@Inject
public WorkspaceSharedPool(@Named("che.workspace.pool.type") String poolType,
        @Named("che.workspace.pool.exact_size") @Nullable String exactSizeProp,
        @Named("che.workspace.pool.cores_multiplier") @Nullable String coresMultiplierProp) {
    ThreadFactory factory = new ThreadFactoryBuilder().setNameFormat("WorkspaceSharedPool-%d")
            .setUncaughtExceptionHandler(LoggingUncaughtExceptionHandler.getInstance()).setDaemon(false)
            .build();//from  w  w  w  .  ja  v  a  2  s  .  com
    switch (poolType.toLowerCase()) {
    case "cached":
        executor = Executors.newCachedThreadPool(factory);
        break;
    case "fixed":
        Integer exactSize = exactSizeProp == null ? null : Ints.tryParse(exactSizeProp);
        int size;
        if (exactSize != null && exactSize > 0) {
            size = exactSize;
        } else {
            size = Runtime.getRuntime().availableProcessors();
            Integer coresMultiplier = coresMultiplierProp == null ? null : Ints.tryParse(coresMultiplierProp);
            if (coresMultiplier != null && coresMultiplier > 0) {
                size *= coresMultiplier;
            }
        }
        executor = Executors.newFixedThreadPool(size, factory);
        break;
    default:
        throw new IllegalArgumentException("The type of the pool '" + poolType + "' is not supported");
    }
}

From source file:co.paralleluniverse.fibers.jdbi.FiberDBI.java

/**
 * Constructor for use with a DataSource which will provide using fixed thread pool executor
 *
 * @param dataSource  may or may not be FiberDataSource
 * @param threadCount/*  w  ww  .  j  a v a2s .  c  o  m*/
 */
public FiberDBI(DataSource dataSource, int threadCount) {
    this(dataSource,
            Executors.newFixedThreadPool(threadCount, new ThreadFactoryBuilder().setDaemon(true).build()));
}