Search in sources :

Example 71 with ThreadFactory

use of java.util.concurrent.ThreadFactory in project wildfly by wildfly.

the class WeldExecutorServices method start.

@Override
public void start(StartContext context) throws StartException {
    final ThreadGroup threadGroup = new ThreadGroup("Weld ThreadGroup");
    final ThreadFactory factory = new JBossThreadFactory(threadGroup, Boolean.FALSE, null, THREAD_NAME_PATTERN, null, null);
    // set TCCL to null for new threads to make sure no deployment classloader leaks through this executor's TCCL
    // Weld does not mind having null TCCL in this executor
    this.executor = Executors.newFixedThreadPool(bound, runnable -> {
        Thread thread = factory.newThread(runnable);
        if (WildFlySecurityManager.isChecking()) {
            AccessController.doPrivileged(new PrivilegedAction<Void>() {

                public Void run() {
                    thread.setContextClassLoader(null);
                    return null;
                }
            });
        } else {
            thread.setContextClassLoader(null);
        }
        return thread;
    });
}
Also used : JBossThreadFactory(org.jboss.threads.JBossThreadFactory) Service(org.jboss.msc.service.Service) StopContext(org.jboss.msc.service.StopContext) StartContext(org.jboss.msc.service.StartContext) PrivilegedAction(java.security.PrivilegedAction) AbstractExecutorServices(org.jboss.weld.executor.AbstractExecutorServices) Executors(java.util.concurrent.Executors) Services(org.jboss.as.server.Services) ExecutorServices(org.jboss.weld.manager.api.ExecutorServices) WildFlySecurityManager(org.wildfly.security.manager.WildFlySecurityManager) ServiceName(org.jboss.msc.service.ServiceName) AccessController(java.security.AccessController) ThreadFactory(java.util.concurrent.ThreadFactory) ExecutorService(java.util.concurrent.ExecutorService) StartException(org.jboss.msc.service.StartException) JBossThreadFactory(org.jboss.threads.JBossThreadFactory) ThreadFactory(java.util.concurrent.ThreadFactory) JBossThreadFactory(org.jboss.threads.JBossThreadFactory) PrivilegedAction(java.security.PrivilegedAction)

Example 72 with ThreadFactory

use of java.util.concurrent.ThreadFactory in project wildfly by wildfly.

the class KeyAffinityServiceFactoryBuilder method build.

@Override
public ServiceBuilder<KeyAffinityServiceFactory> build(ServiceTarget target) {
    int bufferSize = this.bufferSize;
    Function<ExecutorService, KeyAffinityServiceFactory> mapper = executor -> new KeyAffinityServiceFactory() {

        @Override
        public <K> KeyAffinityService<K> createService(Cache<K, ?> cache, KeyGenerator<K> generator) {
            CacheMode mode = cache.getCacheConfiguration().clustering().cacheMode();
            return mode.isDistributed() || mode.isReplicated() ? new KeyAffinityServiceImpl<>(executor, cache, generator, bufferSize, Collections.singleton(cache.getCacheManager().getAddress()), false) : new SimpleKeyAffinityService<>(generator);
        }
    };
    Supplier<ExecutorService> supplier = () -> {
        ThreadGroup threadGroup = new ThreadGroup("KeyAffinityService ThreadGroup");
        String namePattern = "KeyAffinityService Thread Pool -- %t";
        PrivilegedAction<ThreadFactory> action = () -> new JBossThreadFactory(threadGroup, Boolean.FALSE, null, namePattern, null, null);
        return Executors.newCachedThreadPool(doPrivileged(action));
    };
    Service<KeyAffinityServiceFactory> service = new SuppliedValueService<>(mapper, supplier, ExecutorService::shutdown);
    return new AsynchronousServiceBuilder<>(this.getServiceName(), service).startSynchronously().build(target).setInitialMode(ServiceController.Mode.ON_DEMAND);
}
Also used : Service(org.jboss.msc.service.Service) AccessController.doPrivileged(java.security.AccessController.doPrivileged) Cache(org.infinispan.Cache) Function(java.util.function.Function) Supplier(java.util.function.Supplier) KeyGenerator(org.infinispan.affinity.KeyGenerator) SuppliedValueService(org.wildfly.clustering.service.SuppliedValueService) KeyAffinityService(org.infinispan.affinity.KeyAffinityService) KeyAffinityServiceFactory(org.wildfly.clustering.infinispan.spi.affinity.KeyAffinityServiceFactory) ServiceTarget(org.jboss.msc.service.ServiceTarget) ThreadFactory(java.util.concurrent.ThreadFactory) ExecutorService(java.util.concurrent.ExecutorService) Address(org.infinispan.remoting.transport.Address) JBossThreadFactory(org.jboss.threads.JBossThreadFactory) PathAddress(org.jboss.as.controller.PathAddress) ServiceBuilder(org.jboss.msc.service.ServiceBuilder) AsynchronousServiceBuilder(org.wildfly.clustering.service.AsynchronousServiceBuilder) PrivilegedAction(java.security.PrivilegedAction) Executors(java.util.concurrent.Executors) KeyAffinityServiceImpl(org.infinispan.affinity.impl.KeyAffinityServiceImpl) ServiceController(org.jboss.msc.service.ServiceController) CacheMode(org.infinispan.configuration.cache.CacheMode) ServiceName(org.jboss.msc.service.ServiceName) Collections(java.util.Collections) Builder(org.wildfly.clustering.service.Builder) JBossThreadFactory(org.jboss.threads.JBossThreadFactory) CacheMode(org.infinispan.configuration.cache.CacheMode) KeyAffinityServiceFactory(org.wildfly.clustering.infinispan.spi.affinity.KeyAffinityServiceFactory) PrivilegedAction(java.security.PrivilegedAction) ExecutorService(java.util.concurrent.ExecutorService) KeyGenerator(org.infinispan.affinity.KeyGenerator) Cache(org.infinispan.Cache) SuppliedValueService(org.wildfly.clustering.service.SuppliedValueService)

Example 73 with ThreadFactory

use of java.util.concurrent.ThreadFactory in project wildfly by wildfly.

the class ScheduledThreadPoolBuilder method configure.

@Override
public Builder<ThreadPoolConfiguration> configure(OperationContext context, ModelNode model) throws OperationFailedException {
    int maxThreads = this.definition.getMaxThreads().resolveModelAttribute(context, model).asInt();
    long keepAliveTime = this.definition.getKeepAliveTime().resolveModelAttribute(context, model).asLong();
    ThreadPoolExecutorFactory<?> factory = new ThreadPoolExecutorFactory<ScheduledExecutorService>() {

        @Override
        public ScheduledExecutorService createExecutor(ThreadFactory factory) {
            // Use fixed size, based on maxThreads
            ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(maxThreads, new ClassLoaderThreadFactory(factory, ClassLoaderThreadFactory.class.getClassLoader()));
            executor.setKeepAliveTime(keepAliveTime, TimeUnit.MILLISECONDS);
            executor.setRemoveOnCancelPolicy(true);
            executor.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
            return executor;
        }

        @Override
        public void validate() {
        // Do nothing
        }
    };
    this.builder.threadPoolFactory(factory);
    return this;
}
Also used : ClassLoaderThreadFactory(org.wildfly.clustering.service.concurrent.ClassLoaderThreadFactory) ThreadFactory(java.util.concurrent.ThreadFactory) ThreadPoolExecutorFactory(org.infinispan.commons.executors.ThreadPoolExecutorFactory) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) ClassLoaderThreadFactory(org.wildfly.clustering.service.concurrent.ClassLoaderThreadFactory)

Example 74 with ThreadFactory

use of java.util.concurrent.ThreadFactory in project voldemort by voldemort.

the class MetadataStressTest method main.

public static void main(String[] args) throws Exception {
    if (args.length < 3) {
        System.err.println("java voldemort.performance.MetadataStressTest url iterations threads selectors");
        System.exit(-1);
    }
    String url = args[0];
    final int count = Integer.parseInt(args[1]);
    int numThreads = Integer.parseInt(args[2]);
    int numSelectors = args.length > 3 ? Integer.parseInt(args[3]) : 8;
    int timeoutSecs = args.length > 4 ? Integer.parseInt(args[4]) : 10;
    ExecutorService executor = Executors.newFixedThreadPool(numThreads, new ThreadFactory() {

        public Thread newThread(Runnable r) {
            Thread thread = new Thread(r);
            thread.setName("stress-test");
            return thread;
        }
    });
    try {
        final SocketStoreClientFactory factory = new SocketStoreClientFactory(new ClientConfig().setBootstrapUrls(url).setEnableLazy(false).setConnectionTimeout(timeoutSecs, TimeUnit.SECONDS).setSocketTimeout(timeoutSecs, TimeUnit.SECONDS).setMaxThreads(numThreads).setSelectors(numSelectors));
        for (int i = 0; i < numThreads; i++) {
            executor.submit(new Runnable() {

                public void run() {
                    for (int j = 0; j < count; j++) {
                        try {
                            String clusterXml = factory.bootstrapMetadataWithRetries(MetadataStore.CLUSTER_KEY);
                            new ClusterMapper().readCluster(new StringReader(clusterXml));
                            String storesXml = factory.bootstrapMetadataWithRetries(MetadataStore.STORES_KEY);
                            new StoreDefinitionsMapper().readStoreList(new StringReader(storesXml));
                            if (logger.isTraceEnabled())
                                logger.trace("ok " + j);
                        } catch (MappingException me) {
                            logger.fatal(me, me);
                            System.exit(-1);
                        } catch (Exception e) {
                            logger.error(e, e);
                        }
                    }
                }
            });
        }
    } finally {
        executor.shutdown();
    }
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) StoreDefinitionsMapper(voldemort.xml.StoreDefinitionsMapper) ClusterMapper(voldemort.xml.ClusterMapper) MappingException(voldemort.xml.MappingException) MappingException(voldemort.xml.MappingException) SocketStoreClientFactory(voldemort.client.SocketStoreClientFactory) ExecutorService(java.util.concurrent.ExecutorService) StringReader(java.io.StringReader) ClientConfig(voldemort.client.ClientConfig)

Example 75 with ThreadFactory

use of java.util.concurrent.ThreadFactory in project gerrit by GerritCodeReview.

the class StreamCommandExecutorProvider method get.

@Override
public WorkQueue.Executor get() {
    final WorkQueue.Executor executor;
    executor = queues.createQueue(poolSize, "SSH-Stream-Worker");
    final ThreadFactory parent = executor.getThreadFactory();
    executor.setThreadFactory(new ThreadFactory() {

        @Override
        public Thread newThread(final Runnable task) {
            final Thread t = parent.newThread(task);
            t.setPriority(Thread.MIN_PRIORITY);
            return t;
        }
    });
    return executor;
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) WorkQueue(com.google.gerrit.server.git.WorkQueue)

Aggregations

ThreadFactory (java.util.concurrent.ThreadFactory)250 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)47 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)46 ExecutorService (java.util.concurrent.ExecutorService)45 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)35 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)21 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)19 Test (org.junit.Test)17 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)16 Future (java.util.concurrent.Future)15 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)13 ArrayList (java.util.ArrayList)13 LoggingThreadGroup (org.apache.geode.internal.logging.LoggingThreadGroup)12 IOException (java.io.IOException)10 AtomicLong (java.util.concurrent.atomic.AtomicLong)10 ExecutionException (java.util.concurrent.ExecutionException)9 Executor (java.util.concurrent.Executor)9 ChannelFuture (io.netty.channel.ChannelFuture)8 DefaultThreadFactory (io.netty.util.concurrent.DefaultThreadFactory)8 SynchronousQueue (java.util.concurrent.SynchronousQueue)7