Java Code Examples for com.google.common.util.concurrent.ThreadFactoryBuilder#build()

The following examples show how to use com.google.common.util.concurrent.ThreadFactoryBuilder#build() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FirebaseScheduledExecutor.java    From firebase-admin-java with Apache License 2.0 5 votes vote down vote up
private static ThreadFactory decorateThreadFactory(
    ThreadFactory threadFactory, String name, Thread.UncaughtExceptionHandler handler) {
  checkArgument(!Strings.isNullOrEmpty(name));
  ThreadFactoryBuilder builder = new ThreadFactoryBuilder()
      .setThreadFactory(threadFactory)
      .setNameFormat(name)
      .setDaemon(true);
  if (handler != null) {
    builder.setUncaughtExceptionHandler(handler);
  }
  return builder.build();
}
 
Example 2
Source File: ThreadFactories.java    From caravan with Apache License 2.0 5 votes vote down vote up
public static ThreadFactory newDaemonThreadFactory(String nameFormat) {
    ThreadFactoryBuilder builder = new ThreadFactoryBuilder();
    builder.setDaemon(true);
    if (!StringValues.isNullOrWhitespace(nameFormat))
        builder.setNameFormat(nameFormat);
    return builder.build();
}
 
Example 3
Source File: HuaweiScheduledExecutor.java    From MixPush with Apache License 2.0 5 votes vote down vote up
private static ThreadFactory decorateThreadFactory(
        ThreadFactory threadFactory, String name, Thread.UncaughtExceptionHandler handler) {
    checkArgument(!Strings.isNullOrEmpty(name));
    ThreadFactoryBuilder builder = new ThreadFactoryBuilder()
            .setThreadFactory(threadFactory)
            .setNameFormat(name)
            .setDaemon(true);
    if (handler != null) {
        builder.setUncaughtExceptionHandler(handler);
    }
    return builder.build();
}
 
Example 4
Source File: ThreadFactoryProvider.java    From yangtools with Eclipse Public License 1.0 5 votes vote down vote up
public ThreadFactory get() {
    ThreadFactoryBuilder guavaBuilder = new ThreadFactoryBuilder()
            .setNameFormat(namePrefix() + "-%d")
            .setUncaughtExceptionHandler((thread, exception)
                -> logger().error("Thread terminated due to uncaught exception: {}", thread.getName(), exception))
            .setDaemon(daemon());
    priority().ifPresent(guavaBuilder::setPriority);
    logger().info("ThreadFactory created: {}", namePrefix());
    return guavaBuilder.build();
}
 
Example 5
Source File: MultiLangDaemonConfig.java    From amazon-kinesis-client with Apache License 2.0 5 votes vote down vote up
private static ExecutorService buildExecutorService(Properties properties) {
    int maxActiveThreads = getMaxActiveThreads(properties);
    ThreadFactoryBuilder builder = new ThreadFactoryBuilder().setNameFormat("multi-lang-daemon-%04d");
    log.debug("Value for {} property is {}", PROP_MAX_ACTIVE_THREADS, maxActiveThreads);
    if (maxActiveThreads <= 0) {
        log.info("Using a cached thread pool.");
        return new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(),
                builder.build());
    } else {
        log.info("Using a fixed thread pool with {} max active threads.", maxActiveThreads);
        return new ThreadPoolExecutor(maxActiveThreads, maxActiveThreads, 0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>(), builder.build());
    }
}
 
Example 6
Source File: CommitterEventHandler.java    From hadoop with Apache License 2.0 4 votes vote down vote up
@Override
protected void serviceStart() throws Exception {
  ThreadFactoryBuilder tfBuilder = new ThreadFactoryBuilder()
      .setNameFormat("CommitterEvent Processor #%d");
  if (jobClassLoader != null) {
    // if the job classloader is enabled, we need to use the job classloader
    // as the thread context classloader (TCCL) of these threads in case the
    // committer needs to load another class via TCCL
    ThreadFactory backingTf = new ThreadFactory() {
      @Override
      public Thread newThread(Runnable r) {
        Thread thread = new Thread(r);
        thread.setContextClassLoader(jobClassLoader);
        return thread;
      }
    };
    tfBuilder.setThreadFactory(backingTf);
  }
  ThreadFactory tf = tfBuilder.build();
  launcherPool = new ThreadPoolExecutor(5, 5, 1,
      TimeUnit.HOURS, new LinkedBlockingQueue<Runnable>(), tf);
  eventHandlingThread = new Thread(new Runnable() {
    @Override
    public void run() {
      CommitterEvent event = null;
      while (!stopped.get() && !Thread.currentThread().isInterrupted()) {
        try {
          event = eventQueue.take();
        } catch (InterruptedException e) {
          if (!stopped.get()) {
            LOG.error("Returning, interrupted : " + e);
          }
          return;
        }
        // the events from the queue are handled in parallel
        // using a thread pool
        launcherPool.execute(new EventProcessor(event));        }
    }
  });
  eventHandlingThread.setName("CommitterEvent Handler");
  eventHandlingThread.start();
  super.serviceStart();
}
 
Example 7
Source File: CommitterEventHandler.java    From big-c with Apache License 2.0 4 votes vote down vote up
@Override
protected void serviceStart() throws Exception {
  ThreadFactoryBuilder tfBuilder = new ThreadFactoryBuilder()
      .setNameFormat("CommitterEvent Processor #%d");
  if (jobClassLoader != null) {
    // if the job classloader is enabled, we need to use the job classloader
    // as the thread context classloader (TCCL) of these threads in case the
    // committer needs to load another class via TCCL
    ThreadFactory backingTf = new ThreadFactory() {
      @Override
      public Thread newThread(Runnable r) {
        Thread thread = new Thread(r);
        thread.setContextClassLoader(jobClassLoader);
        return thread;
      }
    };
    tfBuilder.setThreadFactory(backingTf);
  }
  ThreadFactory tf = tfBuilder.build();
  launcherPool = new ThreadPoolExecutor(5, 5, 1,
      TimeUnit.HOURS, new LinkedBlockingQueue<Runnable>(), tf);
  eventHandlingThread = new Thread(new Runnable() {
    @Override
    public void run() {
      CommitterEvent event = null;
      while (!stopped.get() && !Thread.currentThread().isInterrupted()) {
        try {
          event = eventQueue.take();
        } catch (InterruptedException e) {
          if (!stopped.get()) {
            LOG.error("Returning, interrupted : " + e);
          }
          return;
        }
        // the events from the queue are handled in parallel
        // using a thread pool
        launcherPool.execute(new EventProcessor(event));        }
    }
  });
  eventHandlingThread.setName("CommitterEvent Handler");
  eventHandlingThread.start();
  super.serviceStart();
}
 
Example 8
Source File: ThreadUtils.java    From yuzhouwan with Apache License 2.0 4 votes vote down vote up
private static ThreadFactory buildThreadFactory(String poolName, Boolean isDaemon) {
    ThreadFactoryBuilder threadFactoryBuilder = new ThreadFactoryBuilder();
    if (!StrUtils.isEmpty(poolName)) threadFactoryBuilder.setNameFormat("[".concat(poolName).concat("]-%d"));
    if (isDaemon != null) threadFactoryBuilder.setDaemon(isDaemon);
    return threadFactoryBuilder.build();
}