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:com.proofpoint.zookeeper.events.QuantizedExecutor.java

/**
 * @param intervalTimeInMs number of ticks to wait after a run request before performing the run
 * @param command the command to run//from  ww w . j a v  a2  s.  c o m
 */
public QuantizedExecutor(long intervalTimeInMs, Runnable command) {
    this.command = command;
    this.intervalTimeInMs = intervalTimeInMs;
    log = Logger.get(command.getClass());

    ThreadFactory factory = new ThreadFactoryBuilder().setDaemon(true)
            .setNameFormat("QuantizedExecutor for " + command.getClass().getName())
            // TODO: .setUncaughtExceptionHandler()
            .build();

    executor = Executors.newSingleThreadExecutor(factory);
    executor.submit(new Runner());
}

From source file:natalia.dymnikova.util.ThreadPoolBeans.java

public ExecutorService firstPriorityTasksExecutor0() {
    final ThreadFactory factory = new ThreadFactoryBuilder().setNameFormat("first-priority-%03d")
            .setDaemon(true).build();/*from  w w w  .  j a v a2  s . com*/

    final ExecutorService executor = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS,
            new SynchronousQueue<>(), factory);

    log.debug("Constructed first priority tasks executor {}", executor);
    return executor;
}

From source file:com.addthis.hydra.job.store.AvailableCache.java

/**
 * Make a cache using specified cache parameters
 *
 * @param refreshMillis How frequently values should be refreshed in milliseconds (if <= 0, no refresh)
 * @param expireMillis  How old values should have to be before they are expired (if <= 0, they never expire)
 * @param maxSize       How many values should be stored in the cache (if <= 0, no explicit limit)
 * @param fetchThreads  How many threads to use to fetch values in the background (if <=0, use two threads)
 *//*from  www  . j  a v  a  2s . com*/
public AvailableCache(long refreshMillis, long expireMillis, int maxSize, int fetchThreads) {
    CacheBuilder<Object, Object> cacheBuilder = CacheBuilder.newBuilder();
    // Configure the cache for any parameters that are > 0
    if (expireMillis > 0) {
        cacheBuilder.expireAfterWrite(expireMillis, TimeUnit.MILLISECONDS);
    }
    if (refreshMillis > 0) {
        cacheBuilder.refreshAfterWrite(refreshMillis, TimeUnit.MILLISECONDS);
    }
    if (maxSize > 0) {
        cacheBuilder.maximumSize(maxSize);
    }
    if (fetchThreads <= 0) {
        fetchThreads = 2;
    }
    executor = new ThreadPoolExecutor(fetchThreads, fetchThreads, 1000L, TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<>(),
            new ThreadFactoryBuilder().setNameFormat("avail-cache-%d").setDaemon(true).build());
    //noinspection unchecked
    this.loadingCache = cacheBuilder.build(new CacheLoader<String, Optional<T>>() {
        @Override
        /**
         * If refreshAfterWrite is enabled, this method is called after returning the old value.
         * The new value will be inserted into the cache when the load() operation completes.
         */
        public ListenableFuture<Optional<T>> reload(final String key, Optional<T> oldValue) {
            ListenableFutureTask<Optional<T>> task = ListenableFutureTask.create(() -> load(key));
            executor.execute(task);
            return task;
        }

        @Override
        public Optional<T> load(String key) throws Exception {
            return Optional.fromNullable(fetchValue(key));
        }
    });
}

From source file:se.svt.helios.serviceregistration.consul.ConsulServiceRegistrar.java

public ConsulServiceRegistrar(final ConsulClient consulClient, final String serviceCheckScript,
        final String serviceCheckInterval) {
    this.consulClient = consulClient;
    this.serviceCheckScript = serviceCheckScript;
    this.serviceCheckInterval = serviceCheckInterval;

    this.handles = Maps.newConcurrentMap();
    this.endpoints = Sets.newConcurrentHashSet();

    this.executor = MoreExecutors.getExitingScheduledExecutorService(
            (ScheduledThreadPoolExecutor) Executors.newScheduledThreadPool(1,
                    new ThreadFactoryBuilder().setNameFormat("consul-registrar-%d").build()),
            0, TimeUnit.SECONDS);

    // If the Consul agent is restarted, all services will forgotten. Therefore we sync the
    // state between services known by this plugin and services registered in Consul.
    Runnable registrationRunnable = new Runnable() {
        @Override//  ww w . ja v a2  s .c o  m
        public void run() {
            syncState();
        }
    };
    this.executor.scheduleAtFixedRate(registrationRunnable, CONSUL_UPDATE_INTERVAL, CONSUL_UPDATE_INTERVAL,
            TimeUnit.SECONDS);
}

From source file:com.pinterest.terrapin.controller.TerrapinControllerServiceImpl.java

public TerrapinControllerServiceImpl(PropertiesConfiguration configuration, ZooKeeperManager zkManager,
        DFSClient hdfsClient, HelixAdmin helixAdmin, String clusterName) {
    this.configuration = configuration;
    this.zkManager = zkManager;
    this.hdfsClient = hdfsClient;
    this.helixAdmin = helixAdmin;
    this.clusterName = clusterName;

    ExecutorService threadPool = new ThreadPoolExecutor(100, 100, 0, TimeUnit.SECONDS,
            new LinkedBlockingDeque<Runnable>(1000),
            new ThreadFactoryBuilder().setDaemon(false).setNameFormat("controller-pool-%d").build());
    this.futurePool = new ExecutorServiceFuturePool(threadPool);
}

From source file:com.twitter.hbc.ClientBuilder.java

public ClientBuilder() {
    enableGZip = true;//from  w  w  w.ja  v a  2 s .  co  m
    name = "hosebird-client-" + clientNum.getAndIncrement();
    ThreadFactory threadFactory = new ThreadFactoryBuilder().setDaemon(true)
            .setNameFormat("hosebird-client-io-thread-%d").build();
    executorService = Executors.newSingleThreadExecutor(threadFactory);

    ThreadFactory rateTrackerThreadFactory = new ThreadFactoryBuilder().setDaemon(true)
            .setNameFormat("hosebird-client-rateTracker-thread-%d").build();

    ScheduledExecutorService scheduledExecutor = Executors.newScheduledThreadPool(1, rateTrackerThreadFactory);
    rateTracker = new BasicRateTracker(30000, 100, true, scheduledExecutor);
    reconnectionManager = new BasicReconnectionManager(5);

    socketTimeoutMillis = 60000;
    connectionTimeoutMillis = 4000;

    schemeRegistry = SchemeRegistryFactory.createDefault();
}

From source file:org.opentripplanner.updater.GraphUpdaterManager.java

/**
 * Constructor//from   www.ja  va2  s.c  o m
 * 
 * @param graph is parent graph of manager
 */
public GraphUpdaterManager(Graph graph) {
    this.graph = graph;

    String routerId = graph.routerId;
    if (routerId == null || routerId.isEmpty())
        routerId = DEFAULT_ROUTER_ID;

    threadFactory = new ThreadFactoryBuilder().setNameFormat("GraphUpdater-" + routerId + "-%d").build();
    scheduler = Executors.newSingleThreadScheduledExecutor(threadFactory);
    updaterPool = Executors.newCachedThreadPool(threadFactory);
}

From source file:com.addthis.hydra.job.spawn.JobOnFinishStateHandlerImpl.java

public JobOnFinishStateHandlerImpl(Spawn spawn) {
    this.spawn = spawn;
    backgroundTaskQueue = new LinkedBlockingQueue<>(backgroundQueueSize);
    backgroundService = new ThreadPoolExecutor(backgroundThreads, backgroundThreads, 0L, TimeUnit.MILLISECONDS,
            backgroundTaskQueue, new ThreadFactoryBuilder().setDaemon(true).build());
    emailLastFired = new AtomicLong();
    backgroundQueueGauge = Metrics.newGauge(Spawn.class, "backgroundExecutorQueue", new Gauge<Integer>() {
        @Override//from   ww  w. j  ava 2  s .c om
        public Integer value() {
            return backgroundTaskQueue.size();
        }
    });
}

From source file:org.graylog2.initializers.BufferSynchronizerService.java

private ExecutorService executorService(MetricRegistry metricRegistry) {
    final ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("buffer-synchronizer-%d")
            .build();//from  w  ww. j a v a 2  s .c o  m
    return new InstrumentedExecutorService(Executors.newSingleThreadExecutor(threadFactory), metricRegistry,
            name(this.getClass(), "executor-service"));
}

From source file:com.google.cloud.bigtable.grpc.io.CallCompletionStatusInterceptor.java

/**
 * Construct an interceptor that uses a single thread for updating statuses.
 *//*from   w w  w . j a v  a  2  s.c om*/
public CallCompletionStatusInterceptor() {
    this(Executors.newSingleThreadExecutor(
            new ThreadFactoryBuilder().setNameFormat("call-status-recorder").setDaemon(true).build()));
}