Example usage for java.util.concurrent Executors defaultThreadFactory

List of usage examples for java.util.concurrent Executors defaultThreadFactory

Introduction

In this page you can find the example usage for java.util.concurrent Executors defaultThreadFactory.

Prototype

public static ThreadFactory defaultThreadFactory() 

Source Link

Document

Returns a default thread factory used to create new threads.

Usage

From source file:org.apache.phoenix.log.QueryLoggerDisruptor.java

public QueryLoggerDisruptor(Configuration configuration) throws SQLException {
    WaitStrategy waitStrategy;/*from   w w w.  j  a v a2  s  .  co m*/
    try {
        waitStrategy = (WaitStrategy) Class
                .forName(configuration.get(QueryServices.LOG_BUFFER_WAIT_STRATEGY, DEFAULT_WAIT_STRATEGY))
                .newInstance();
    } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
        throw new SQLException(e);
    }

    ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("QueryLogger" + "-thread-%s")
            .setDaemon(true).setThreadFactory(new ThreadFactory() {
                @Override
                public Thread newThread(Runnable r) {
                    final Thread result = Executors.defaultThreadFactory().newThread(r);
                    result.setContextClassLoader(QueryLoggerDisruptor.class.getClass().getClassLoader());
                    return result;
                }
            }).build();
    disruptor = new Disruptor<RingBufferEvent>(RingBufferEvent.FACTORY,
            configuration.getInt(QueryServices.LOG_BUFFER_SIZE, RING_BUFFER_SIZE), threadFactory,
            ProducerType.MULTI, waitStrategy);
    final ExceptionHandler<RingBufferEvent> errorHandler = new QueryLoggerDefaultExceptionHandler();
    disruptor.setDefaultExceptionHandler(errorHandler);

    final QueryLogDetailsEventHandler[] handlers = { new QueryLogDetailsEventHandler(configuration) };
    disruptor.handleEventsWith(handlers);
    LOG.info("Starting  QueryLoggerDisruptor for with ringbufferSize="
            + disruptor.getRingBuffer().getBufferSize() + ", waitStrategy="
            + waitStrategy.getClass().getSimpleName() + ", " + "exceptionHandler=" + errorHandler + "...");
    disruptor.start();

}

From source file:io.github.mmichaelis.selenium.client.provider.AbstractWebDriverProviderTest.java

@Test
public void quit_ends_browser() throws Exception {
    final WebDriverProvider driverProvider = new NoExceptionWebDriverProvider(defaultDriver);
    final Thread thread = Executors.defaultThreadFactory().newThread(new Runnable() {
        @Override//from   w ww .  ja  v a  2 s .  c om
        public void run() {
            driverProvider.get();
            driverProvider.quit();
        }
    });
    thread.start();
    thread.join();
    verify(defaultDriver, atLeastOnce()).quit();
}

From source file:org.apache.nifi.remote.client.http.HttpClient.java

public HttpClient(final SiteToSiteClientConfig config) {
    super(config);

    peerSelector = new PeerSelector(this, config.getPeerPersistenceFile());
    peerSelector.setEventReporter(config.getEventReporter());

    taskExecutor = Executors.newScheduledThreadPool(1, new ThreadFactory() {
        private final ThreadFactory defaultFactory = Executors.defaultThreadFactory();

        @Override/* www. j  a v  a  2 s. c om*/
        public Thread newThread(final Runnable r) {
            final Thread thread = defaultFactory.newThread(r);
            thread.setName("Http Site-to-Site PeerSelector");
            thread.setDaemon(true);
            return thread;
        }
    });

    taskExecutor.scheduleWithFixedDelay(new Runnable() {
        @Override
        public void run() {
            peerSelector.refreshPeers();
        }
    }, 0, 5, TimeUnit.SECONDS);

}

From source file:org.apache.http.impl.nio.client.MinimalHttpAsyncClient.java

public MinimalHttpAsyncClient(final NHttpClientConnectionManager connmgr, final HttpProcessor httpProcessor) {
    this(connmgr, Executors.defaultThreadFactory(), new HttpAsyncRequestExecutor(), httpProcessor,
            DefaultConnectionReuseStrategy.INSTANCE, DefaultConnectionKeepAliveStrategy.INSTANCE);
}

From source file:org.apache.http.HC4.impl.nio.client.MinimalHttpAsyncClient.java

public MinimalHttpAsyncClient(final NHttpClientConnectionManager connmgr, final HttpProcessor httpProcessor) {
    this(connmgr, Executors.defaultThreadFactory(), new LoggingAsyncRequestExecutor(), httpProcessor,
            DefaultConnectionReuseStrategy.INSTANCE, DefaultConnectionKeepAliveStrategy.INSTANCE);
}

From source file:io.github.mmichaelis.selenium.client.provider.AbstractWebDriverProviderTest.java

@Test
public void quit_if_not_started_does_nothing() throws Exception {
    final WebDriverProvider driverProvider = new NoExceptionWebDriverProvider(defaultDriver);
    final Thread thread = Executors.defaultThreadFactory().newThread(new Runnable() {
        @Override//from   w  w w .  ja va  2 s .c o  m
        public void run() {
            driverProvider.quit();
        }
    });
    thread.start();
    thread.join();
    verify(defaultDriver, never()).quit();
}

From source file:org.springframework.cloud.stream.micrometer.DefaultDestinationPublishingMeterRegistry.java

@Override
public void start() {
    start(Executors.defaultThreadFactory());
}

From source file:net.sourceforge.subsonic.service.PodcastService.java

public PodcastService() {
    ThreadFactory threadFactory = new ThreadFactory() {
        public Thread newThread(Runnable r) {
            Thread t = Executors.defaultThreadFactory().newThread(r);
            t.setDaemon(true);// w ww  .j a v  a  2 s  . c o  m
            return t;
        }
    };
    refreshExecutor = Executors.newFixedThreadPool(5, threadFactory);
    downloadExecutor = Executors.newFixedThreadPool(3, threadFactory);
    scheduledExecutor = Executors.newSingleThreadScheduledExecutor(threadFactory);
}

From source file:com.codefollower.lealone.omid.tso.TSOServer.java

@Override
public void run() {
    // *** Start the Netty configuration ***
    // Start server with Nb of active threads = 2*NB CPU + 1 as maximum.
    ChannelFactory factory = new NioServerSocketChannelFactory(Executors.newCachedThreadPool(),
            Executors.newCachedThreadPool(), (Runtime.getRuntime().availableProcessors() * 2 + 1) * 2);

    ServerBootstrap bootstrap = new ServerBootstrap(factory);
    // Create the global ChannelGroup
    ChannelGroup channelGroup = new DefaultChannelGroup(TSOServer.class.getName());
    // threads max
    // int maxThreads = Runtime.getRuntime().availableProcessors() *2 + 1;
    int maxThreads = 5;
    // Memory limitation: 1MB by channel, 1GB global, 100 ms of timeout
    ThreadPoolExecutor pipelineExecutor = new OrderedMemoryAwareThreadPoolExecutor(maxThreads, 1048576,
            1073741824, 100, TimeUnit.MILLISECONDS, new ObjectSizeEstimator() {
                @Override/*from w  ww .ja va2s.  c  o  m*/
                public int estimateSize(Object o) {
                    return 1000;
                }
            }, Executors.defaultThreadFactory());

    state = BookKeeperStateBuilder.getState(config);
    if (state == null) {
        LOG.error("Couldn't build state");
        return;
    }

    state.addRecord(new byte[] { LoggerProtocol.LOG_START }, new AddRecordCallback() {
        @Override
        public void addRecordComplete(int rc, Object ctx) {
        }
    }, null);

    LOG.info("PARAM MAX_ITEMS: " + TSOState.MAX_ITEMS);
    LOG.info("PARAM BATCH_SIZE: " + config.getBatchSize());
    LOG.info("PARAM MAX_THREADS: " + maxThreads);

    final TSOHandler handler = new TSOHandler(channelGroup, state, config.getBatchSize());
    handler.start();

    bootstrap.setPipelineFactory(new TSOPipelineFactory(pipelineExecutor, handler));
    bootstrap.setOption("tcpNoDelay", false);
    //setting buffer size can improve I/O
    bootstrap.setOption("child.sendBufferSize", 1048576);
    bootstrap.setOption("child.receiveBufferSize", 1048576);
    // better to have an receive buffer predictor
    bootstrap.setOption("receiveBufferSizePredictorFactory", new AdaptiveReceiveBufferSizePredictorFactory());
    //if the server is sending 1000 messages per sec, optimum write buffer water marks will
    //prevent unnecessary throttling, Check NioSocketChannelConfig doc
    bootstrap.setOption("writeBufferLowWaterMark", 32 * 1024);
    bootstrap.setOption("writeBufferHighWaterMark", 64 * 1024);

    bootstrap.setOption("child.tcpNoDelay", false);
    bootstrap.setOption("child.keepAlive", true);
    bootstrap.setOption("child.reuseAddress", true);
    bootstrap.setOption("child.connectTimeoutMillis", 60000);

    // *** Start the Netty running ***

    // Create the monitor
    ThroughputMonitor monitor = new ThroughputMonitor(state);
    // Add the parent channel to the group
    Channel channel = bootstrap.bind(new InetSocketAddress(config.getPort()));
    channelGroup.add(channel);

    // Compacter handler
    ChannelFactory comFactory = new NioServerSocketChannelFactory(Executors.newCachedThreadPool(),
            Executors.newCachedThreadPool(), (Runtime.getRuntime().availableProcessors() * 2 + 1) * 2);
    ServerBootstrap comBootstrap = new ServerBootstrap(comFactory);
    ChannelGroup comGroup = new DefaultChannelGroup("compacter");
    final CompacterHandler comHandler = new CompacterHandler(comGroup, state);
    comBootstrap.setPipelineFactory(new ChannelPipelineFactory() {

        @Override
        public ChannelPipeline getPipeline() throws Exception {
            ChannelPipeline pipeline = Channels.pipeline();
            pipeline.addLast("decoder", new ObjectDecoder());
            pipeline.addLast("encoder", new ObjectEncoder());
            pipeline.addLast("handler", comHandler);
            return pipeline;
        }
    });
    comBootstrap.setOption("tcpNoDelay", false);
    comBootstrap.setOption("child.tcpNoDelay", false);
    comBootstrap.setOption("child.keepAlive", true);
    comBootstrap.setOption("child.reuseAddress", true);
    comBootstrap.setOption("child.connectTimeoutMillis", 100);
    comBootstrap.setOption("readWriteFair", true);
    channel = comBootstrap.bind(new InetSocketAddress(config.getPort() + 1));

    // Starts the monitor
    monitor.start();
    synchronized (lock) {
        while (!finish) {
            try {
                lock.wait();
            } catch (InterruptedException e) {
                break;
            }
        }
    }

    handler.stop();
    comHandler.stop();
    state.stop();

    // *** Start the Netty shutdown ***

    // End the monitor
    LOG.info("End of monitor");
    monitor.interrupt();
    // Now close all channels
    LOG.info("End of channel group");
    channelGroup.close().awaitUninterruptibly();
    comGroup.close().awaitUninterruptibly();
    // Close the executor for Pipeline
    LOG.info("End of pipeline executor");
    pipelineExecutor.shutdownNow();
    // Now release resources
    LOG.info("End of resources");
    factory.releaseExternalResources();
    comFactory.releaseExternalResources();
}

From source file:com.google.devtools.build.lib.bazel.dash.DashModule.java

public DashModule() {
    // Make sure sender != null before we hop on the event bus.
    sender = NO_OP_SENDER;// w  w  w.j a  v a2  s . c  om
    executorService = Executors.newFixedThreadPool(5, new ThreadFactory() {
        @Override
        public Thread newThread(Runnable runnable) {
            Thread thread = Executors.defaultThreadFactory().newThread(runnable);
            thread.setDaemon(true);
            return thread;
        }
    });
}