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

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

Introduction

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

Prototype

public ThreadFactoryBuilder setNameFormat(String nameFormat) 

Source Link

Document

Sets the naming format to use when naming threads ( Thread#setName ) which are created with this ThreadFactory.

Usage

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

/**
 * @param args//from  w  ww. ja v a2s. c  o m
 * @throws IOException
 * @throws NumberFormatException
 * @throws InterruptedException
 */
public static void main(String[] args) throws NumberFormatException, IOException, InterruptedException {
    if (args.length != 3) {
        System.err.println("USAGE: BookieClient bookieHost port ledger#");
        return;
    }
    WriteCallback cb = new WriteCallback() {

        public void writeComplete(int rc, long ledger, long entry, BookieSocketAddress addr, Object ctx) {
            Counter counter = (Counter) ctx;
            counter.dec();
            if (rc != 0) {
                System.out.println("rc = " + rc + " for " + entry + "@" + ledger);
            }
        }
    };
    Counter counter = new Counter();
    byte hello[] = "hello".getBytes(UTF_8);
    long ledger = Long.parseLong(args[2]);
    ThreadFactoryBuilder tfb = new ThreadFactoryBuilder();
    ClientSocketChannelFactory channelFactory = new NioClientSocketChannelFactory(
            Executors.newCachedThreadPool(tfb.setNameFormat("BookKeeper-NIOBoss-%d").build()),
            Executors.newCachedThreadPool(tfb.setNameFormat("BookKeeper-NIOWorker-%d").build()));
    OrderedSafeExecutor executor = OrderedSafeExecutor.newBuilder().name("BookieClientWorker").numThreads(1)
            .build();
    BookieClient bc = new BookieClient(new ClientConfiguration(), channelFactory, executor);
    BookieSocketAddress addr = new BookieSocketAddress(args[0], Integer.parseInt(args[1]));

    for (int i = 0; i < 100000; i++) {
        counter.inc();
        bc.addEntry(addr, ledger, new byte[0], i, ChannelBuffers.wrappedBuffer(hello), cb, counter, 0);
    }
    counter.wait(0);
    System.out.println("Total = " + counter.total());
    channelFactory.releaseExternalResources();
    executor.shutdown();
}

From source file:com.aerofs.baseline.Threads.java

/**
 * Create a new {@link java.util.concurrent.ThreadFactory} with
 * threads named according to {@code nameFormat}.
 * have names/* ww w. ja  va  2s.  c o  m*/
 *
 * @param nameFormat a {@link String#format(String, Object...)}-compatible
 *                   format String, to which a unique, sequential integer (0, 1, etc.)
 *                   will be supplied as the single parameter
 * @return a valid {@code ThreadFactory} with threads whose {@link Thread#getName}
 * returns the name specified by {@code nameFormat}
 *
 * @see com.google.common.util.concurrent.ThreadFactoryBuilder#setNameFormat
 */
public static ThreadFactory newNamedThreadFactory(String nameFormat) {
    ThreadFactoryBuilder builder = new ThreadFactoryBuilder();
    return builder.setNameFormat(nameFormat).setUncaughtExceptionHandler(new DefaultUncaughtExceptionHandler())
            .setThreadFactory(Executors.defaultThreadFactory()).build();
}

From source file:org.jdag.common.NamedThreadFactory.java

/**
 * Returns a <code>ThreadFactory that creates threads with the simple
 * name of the class.//  w w w  .  j  av  a2s .  c om
 */
public static ThreadFactory newNamedFactory(Class<?> hostClass) {
    ThreadFactoryBuilder tfBuilder = new ThreadFactoryBuilder();
    ThreadFactory namedFactory = tfBuilder.setNameFormat(hostClass.getSimpleName()).build();
    return namedFactory;
}

From source file:org.jdag.common.NamedThreadFactory.java

/**
 * Returns a <code>ThreadFactory that creates threads with the given simple
 * name./*www .ja v  a2  s  .c  o m*/
 */
public static ThreadFactory newNamedFactory(String simpleName) {
    ThreadFactoryBuilder tfBuilder = new ThreadFactoryBuilder();
    ThreadFactory namedFactory = tfBuilder.setNameFormat(simpleName).build();
    return namedFactory;
}

From source file:org.apache.tajo.rpc.NettyUtils.java

public static EventLoopGroup createEventLoopGroup(String name, int threads) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Create " + name + " EventLoopGroup. threads:" + threads);
    }/*from  w w w .j a v a2 s. co m*/

    ThreadFactoryBuilder builder = new ThreadFactoryBuilder();
    ThreadFactory clientFactory = builder.setNameFormat(name + " #%d").build();

    return createEventLoopGroup(threads, clientFactory);
}

From source file:org.apache.tajo.rpc.RpcChannelFactory.java

protected static EventLoopGroup createClientEventloopGroup(String name, int workerNum) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Create " + name + " ClientEventLoopGroup. Worker:" + workerNum);
    }//from  w  w  w . j  ava 2 s .com

    ThreadFactoryBuilder builder = new ThreadFactoryBuilder();
    ThreadFactory clientFactory = builder.setNameFormat(name + " Client #%d").build();

    return new NioEventLoopGroup(workerNum, clientFactory);
}

From source file:org.apache.tajo.rpc.RpcChannelFactory.java

public static ServerBootstrap createServerChannelFactory(String name, int workerNum) {
    name = name + "-" + serverCount.incrementAndGet();
    if (LOG.isInfoEnabled()) {
        LOG.info("Create " + name + " ServerSocketChannelFactory. Worker:" + workerNum);
    }/*from   www  . j  av  a2s . c o  m*/
    ThreadFactoryBuilder builder = new ThreadFactoryBuilder();
    ThreadFactory bossFactory = builder.setNameFormat(name + " Server Boss #%d").build();
    ThreadFactory workerFactory = builder.setNameFormat(name + " Server Worker #%d").build();

    EventLoopGroup bossGroup = new NioEventLoopGroup(1, bossFactory);
    EventLoopGroup workerGroup = new NioEventLoopGroup(workerNum, workerFactory);

    return new ServerBootstrap().group(bossGroup, workerGroup);
}

From source file:org.apache.hadoop.hbase.thrift2.ThriftServer.java

private static ExecutorService createExecutor(int workerThreads, ThriftMetrics metrics) {
    CallQueue callQueue = new CallQueue(new LinkedBlockingQueue<Call>(), metrics);
    ThreadFactoryBuilder tfb = new ThreadFactoryBuilder();
    tfb.setDaemon(true);/*from   w ww . ja  va2  s .com*/
    tfb.setNameFormat("thrift2-worker-%d");
    return new ThreadPoolExecutor(workerThreads, workerThreads, Long.MAX_VALUE, TimeUnit.SECONDS, callQueue,
            tfb.build());
}

From source file:gobblin.util.ExecutorsUtils.java

private static ThreadFactory newThreadFactory(ThreadFactoryBuilder builder, Optional<Logger> logger,
        Optional<String> nameFormat) {
    if (nameFormat.isPresent()) {
        builder.setNameFormat(nameFormat.get());
    }/*from  w w w .  j a  v  a  2  s.  c  o  m*/
    return builder.setUncaughtExceptionHandler(new LoggingUncaughtExceptionHandler(logger)).build();
}

From source file:org.metaservice.kryo.run.KryoServer.java

public KryoServer() {
    ThreadFactoryBuilder threadFactoryBuilder = new ThreadFactoryBuilder();
    threadFactoryBuilder.setNameFormat("objectSpace-%d");
    objectSpaceExecutor = Executors.newFixedThreadPool(3, threadFactoryBuilder.build());
    threadFactoryBuilder.setNameFormat("mongoWriter-%d");
    mongoWriterExecutor = Executors.newFixedThreadPool(10, threadFactoryBuilder.build());
}