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.marketcetera.util.rpc.RpcServer.java

@Override
@PostConstruct//from w w w. ja  v  a 2s .com
public synchronized void start() {
    Validate.notNull(hostname);
    Validate.isTrue(port > 0 && port < 65536);
    Validate.notNull(sessionManager);
    Validate.notNull(authenticator);
    Validate.isTrue(threadPoolCore > 0);
    Validate.isTrue(threadPoolMax > 0);
    Validate.isTrue(threadPoolMax >= threadPoolCore);
    Validate.isTrue(sendBufferSize > 0);
    Validate.isTrue(receiveBufferSize > 0);
    Validate.notEmpty(serviceSpecs);
    Messages.SERVER_STARTING.info(this, hostname, port);
    if (isRunning()) {
        stop();
    }
    try {
        reportContext = JAXBContext.newInstance(
                contextClassProvider == null ? new Class<?>[0] : contextClassProvider.getContextClasses());
        marshaller = reportContext.createMarshaller();
        unmarshaller = reportContext.createUnmarshaller();
    } catch (JAXBException e) {
        SLF4JLoggerProxy.error(this, e);
        throw new RuntimeException(e);
    }
    PeerInfo serverInfo = new PeerInfo(getRpcHostname(), getRpcPort());
    executor = new ThreadPoolCallExecutor(threadPoolCore, threadPoolMax);
    DuplexTcpServerPipelineFactory serverFactory = new DuplexTcpServerPipelineFactory(serverInfo);
    serverFactory.setRpcServerCallExecutor(executor);
    ServerBootstrap bootstrap = new ServerBootstrap();
    bootstrap.group(
            new NioEventLoopGroup(0, new RenamingThreadFactoryProxy("boss", Executors.defaultThreadFactory())),
            new NioEventLoopGroup(0,
                    new RenamingThreadFactoryProxy("worker", Executors.defaultThreadFactory())));
    bootstrap.channel(NioServerSocketChannel.class);
    bootstrap.childHandler(serverFactory);
    bootstrap.localAddress(serverInfo.getPort());
    bootstrap.option(ChannelOption.SO_SNDBUF, sendBufferSize);
    bootstrap.option(ChannelOption.SO_RCVBUF, receiveBufferSize);
    bootstrap.childOption(ChannelOption.SO_RCVBUF, receiveBufferSize);
    bootstrap.childOption(ChannelOption.SO_SNDBUF, sendBufferSize);
    bootstrap.option(ChannelOption.TCP_NODELAY, noDelay);
    for (RpcServiceSpec<SessionClazz> serviceSpec : serviceSpecs) {
        serviceSpec.setRpcServerServices(this);
        BlockingService activeService = serviceSpec.generateService();
        serverFactory.getRpcServiceRegistry().registerService(activeService);
        Messages.SERVICE_STARTING.info(this, serviceSpec.getDescription());
    }
    channelToken = bootstrap.bind();
    while (!channelToken.isDone()) {
        try {
            Thread.sleep(250);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
    // TODO throw exception?
    running.set(channelToken.isSuccess());
    //RpcClientConnectionRegistry clientRegistry = new RpcClientConnectionRegistry();
    //serverFactory.registerConnectionEventListener(clientRegistry);
}

From source file:com.taobao.metamorphosis.gregor.slave.OrderedThreadPoolExecutor.java

/**
 * Creates a default ThreadPool, with default values : - minimum pool size
 * is 0 - keepAlive set to 30 seconds - A default ThreadFactory - All events
 * are accepted//from  www .j  a  v  a 2 s  .  com
 * 
 * @param maximumPoolSize
 *            The maximum pool size
 */
public OrderedThreadPoolExecutor(final int maximumPoolSize) {
    this(DEFAULT_INITIAL_THREAD_POOL_SIZE, maximumPoolSize, DEFAULT_KEEP_ALIVE, TimeUnit.SECONDS,
            Executors.defaultThreadFactory());
}

From source file:org.rhq.core.system.SigarAccessHandler.java

SigarAccessHandler(SigarFactory sigarFactory) {
    this.sigarFactory = sigarFactory;
    sharedSigarLock = new ReentrantLock();
    localSigarLock = new ReentrantLock();
    scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {

        private ThreadFactory defaultThreadFactory = Executors.defaultThreadFactory();

        private AtomicInteger threadCounter = new AtomicInteger(0);

        @Override/*  www. j  a v a2  s. c  o m*/
        public Thread newThread(Runnable runnable) {
            Thread thread = defaultThreadFactory.newThread(runnable);
            thread.setName("SigarAccessHandler-" + threadCounter.incrementAndGet());
            // With daemon threads, there is no need to call #shutdown on the executor to let the JVM go down
            thread.setDaemon(true);
            return thread;
        }
    });
    scheduledExecutorService.scheduleWithFixedDelay(new ThresholdChecker(), 1, 5, MINUTES);
    localSigarInstancesCount = 0;
    closed = false;
}

From source file:edu.umass.cs.gigapaxos.FailureDetection.java

FailureDetection(NodeIDType id, InterfaceNIOTransport<NodeIDType, JSONObject> niot, String paxosLogFolder) {
    nioTransport = niot;//from www  . j  ava 2  s. c  om
    myID = id;
    this.execpool = Executors.newScheduledThreadPool(1, new ThreadFactory() {
        @Override
        public Thread newThread(Runnable r) {
            Thread thread = Executors.defaultThreadFactory().newThread(r);
            thread.setName(FailureDetection.class.getSimpleName() + myID);
            return thread;
        }
    });
    lastHeardFrom = new ConcurrentHashMap<NodeIDType, Long>();
    keepAliveTargets = new TreeSet<NodeIDType>();
    futures = new HashMap<NodeIDType, ScheduledFuture<PingTask>>();
    initialize(paxosLogFolder);
}

From source file:org.pentaho.osgi.i18n.impl.LocalizationManager.java

public void bundleChanged(Bundle bundle) throws IOException, ParseException {
    boolean rebuildCache;
    synchronized (configMap) {
        rebuildCache = configMap.remove(bundle.getBundleId()) != null;
    }//  ww  w  .  ja va 2  s .c o m

    if (bundle.getState() == Bundle.ACTIVE) {
        Map<String, OSGIResourceBundleFactory> configEntry = new HashMap<String, OSGIResourceBundleFactory>();
        OSGIResourceBundleFactory bundleFactory;
        Enumeration<URL> urlEnumeration = bundle.findEntries(OSGIResourceNamingConvention.RESOURCES_ROOT_FOLDER,
                "*" + OSGIResourceNamingConvention.RESOURCES_DEFAULT_EXTENSION + "*", true);
        if (urlEnumeration != null) {
            while (urlEnumeration.hasMoreElements()) {
                URL url = urlEnumeration.nextElement();
                if (url != null) {
                    String fileName = url.getFile();
                    String relativeName = fileName;
                    String name = getPropertyName(fileName);
                    int priority = OSGIResourceNamingConvention.getPropertyPriority(fileName);
                    bundleFactory = new OSGIResourceBundleFactory(name, relativeName, url, priority);
                    configEntry.put(relativeName, bundleFactory);
                    rebuildCache = true;
                }
            }
        }

        if (!configEntry.isEmpty()) {
            synchronized (configMap) {
                configMap.put(bundle.getBundleId(), configEntry);
            }
            rebuildCache = true;
        }
    }
    if (rebuildCache) {
        synchronized (configMap) {
            if (executorService == null) {
                executorService = Executors.newSingleThreadExecutor(new ThreadFactory() {
                    @Override
                    public Thread newThread(Runnable r) {
                        Thread thread = Executors.defaultThreadFactory().newThread(r);
                        thread.setDaemon(true);
                        thread.setName("Localization pool");
                        return thread;
                    }
                });
            }
            cache = executorService.submit(new OSGIResourceBundleCacheCallable(
                    new HashMap<Long, Map<String, OSGIResourceBundleFactory>>(configMap)));
        }
    }
}

From source file:com.asprise.imaging.core.Imaging.java

/** Use this executor service to make sure that all scanning related code is executed from the same thread. */
public static ExecutorService getDefaultExecutorServiceForScanning() {
    if (executorServiceForScanning == null) {
        synchronized (Imaging.class) {
            if (executorServiceForScanning == null) {
                executorServiceForScanning = Executors.newSingleThreadExecutor(new ThreadFactory() { // custom factory for user-friendly thread name
                    final AtomicInteger threadNumber = new AtomicInteger(1);
                    ThreadFactory defaultThreadFactory = Executors.defaultThreadFactory();

                    @Override//from   ww  w .  j ava2 s.c  o  m
                    public Thread newThread(Runnable r) {
                        Thread thread = defaultThreadFactory.newThread(r);
                        thread.setName("scan" + (threadNumber.get() == 1 ? "" : "-" + threadNumber));
                        return thread;
                    }
                });
            }
        }
    }
    return executorServiceForScanning;
}

From source file:edu.umass.cs.nio.AbstractPacketDemultiplexer.java

/**
 * /*from  w  ww  .  j a v a  2 s  . c o m*/
 * @param threadPoolSize
 *            Refer documentation for {@link #setThreadPoolSize(int)
 *            setThreadPoolsize(int)}.
 */
public AbstractPacketDemultiplexer(int threadPoolSize) {
    this.executor = (ScheduledThreadPoolExecutor) Executors.newScheduledThreadPool(threadPoolSize,
            new ThreadFactory() {
                @Override
                public Thread newThread(Runnable r) {
                    Thread thread = Executors.defaultThreadFactory().newThread(r);
                    thread.setName(threadName);
                    return thread;
                }
            });
    this.myThreadPoolSize = threadPoolSize;
}

From source file:org.sipfoundry.sipxconfig.commserver.imdb.ReplicationTrigger.java

private void replicateEntityGroup(Runnable worker) {
    if (m_executorService == null) {
        m_executorService = Executors.newSingleThreadExecutor(Executors.defaultThreadFactory());
    }//from w  w  w.  j a v  a2s . c om
    m_executorService.submit(worker);
    m_executorService.shutdown();
    m_executorService = null;
}

From source file:com.azaptree.services.executor.ThreadPoolConfig.java

@NotNull
public ThreadFactory getThreadFactory() {
    if (StringUtils.isBlank(name) && !daemon) {
        return Executors.defaultThreadFactory();
    }/*from w  ww. ja v  a2  s. c  o  m*/

    return new ThreadFactory() {
        private final AtomicInteger threadCounter = new AtomicInteger(0);

        @Override
        public Thread newThread(final Runnable r) {
            final Thread t = new Thread(r, String.format("%s-%d", name, threadCounter.incrementAndGet()));
            t.setDaemon(daemon);
            return t;
        }
    };
}

From source file:com.taobao.metamorphosis.gregor.slave.OrderedThreadPoolExecutor.java

/**
 * Creates a default ThreadPool, with default values : - keepAlive set to 30
 * seconds - A default ThreadFactory - All events are accepted
 * /*  w w  w .  j  av a  2  s.c o m*/
 * @param corePoolSize
 *            The initial pool sizePoolSize
 * @param maximumPoolSize
 *            The maximum pool size
 */
public OrderedThreadPoolExecutor(final int corePoolSize, final int maximumPoolSize) {
    this(corePoolSize, maximumPoolSize, DEFAULT_KEEP_ALIVE, TimeUnit.SECONDS, Executors.defaultThreadFactory());
}