Search in sources :

Example 66 with ThreadFactory

use of java.util.concurrent.ThreadFactory in project imcache by Cetsoft.

the class ConcurrentEvictionListener method init.

/**
     * Inits the.
     *
     * @param queueSize the queue size
     * @param concurrencyLevel the concurrency level
     */
protected void init(int queueSize, int concurrencyLevel) {
    cacheTasks = new ArrayBlockingQueue<CacheTask<K, V>>(queueSize);
    ExecutorService drainerService = Executors.newFixedThreadPool(concurrencyLevel, new ThreadFactory() {

        public Thread newThread(Runnable runnable) {
            return ThreadUtils.createDaemonThread(runnable, "imcache:concurrentAsyncEvictionDrainer(thread=" + NO_OF_EVICTION_DRAINERS.incrementAndGet() + ")");
        }
    });
    // Creates runnables to drain cache task queue constantly.
    for (int i = 0; i < concurrencyLevel; i++) {
        drainerService.execute(new Runnable() {

            public void run() {
                while (true) {
                    drainQueue();
                }
            }
        });
    }
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) ExecutorService(java.util.concurrent.ExecutorService)

Example 67 with ThreadFactory

use of java.util.concurrent.ThreadFactory in project pulsar by yahoo.

the class MockBrokerService method startMockBrokerService.

public void startMockBrokerService() throws Exception {
    ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("mock-pulsar-%s").build();
    final int numThreads = 2;
    final int MaxMessageSize = 5 * 1024 * 1024;
    EventLoopGroup eventLoopGroup;
    try {
        if (SystemUtils.IS_OS_LINUX) {
            try {
                eventLoopGroup = new EpollEventLoopGroup(numThreads, threadFactory);
            } catch (UnsatisfiedLinkError e) {
                eventLoopGroup = new NioEventLoopGroup(numThreads, threadFactory);
            }
        } else {
            eventLoopGroup = new NioEventLoopGroup(numThreads, threadFactory);
        }
        workerGroup = eventLoopGroup;
        ServerBootstrap bootstrap = new ServerBootstrap();
        bootstrap.group(workerGroup, workerGroup);
        if (workerGroup instanceof EpollEventLoopGroup) {
            bootstrap.channel(EpollServerSocketChannel.class);
        } else {
            bootstrap.channel(NioServerSocketChannel.class);
        }
        bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {

            @Override
            public void initChannel(SocketChannel ch) throws Exception {
                ch.pipeline().addLast("frameDecoder", new LengthFieldBasedFrameDecoder(MaxMessageSize, 0, 4, 0, 4));
                ch.pipeline().addLast("handler", new MockServerCnx());
            }
        });
        // Bind and start to accept incoming connections.
        bootstrap.bind(brokerServicePort).sync();
    } catch (Exception e) {
        throw e;
    }
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) EpollServerSocketChannel(io.netty.channel.epoll.EpollServerSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) EpollEventLoopGroup(io.netty.channel.epoll.EpollEventLoopGroup) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) EpollEventLoopGroup(io.netty.channel.epoll.EpollEventLoopGroup) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) LengthFieldBasedFrameDecoder(io.netty.handler.codec.LengthFieldBasedFrameDecoder) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 68 with ThreadFactory

use of java.util.concurrent.ThreadFactory in project jdk8u_jdk by JetBrains.

the class AsyncCloseAndInterrupt method main.

public static void main(String[] args) throws Exception {
    wildcardAddress = new InetSocketAddress(InetAddress.getLocalHost(), 0);
    initAcceptor();
    if (!TestUtil.onWindows())
        initRefuser();
    initPipes();
    initFile();
    if (TestUtil.onWindows()) {
        log.println("WARNING: Cannot test FileChannel transfer operations" + " on Windows");
    } else {
        test(diskFileChannelFactory, TRANSFER_TO);
        test(diskFileChannelFactory, TRANSFER_FROM);
    }
    if (fifoFile != null)
        test(fifoFileChannelFactory);
    // Testing positional file reads and writes is impractical: It requires
    // access to a large file soft-mounted via NFS, and even then isn't
    // completely guaranteed to work.
    //
    // Testing map is impractical and arguably unnecessary: It's
    // unclear under what conditions mmap(2) will actually block.
    test(connectedSocketChannelFactory);
    if (TestUtil.onWindows()) {
        log.println("WARNING Cannot reliably test connect/finishConnect" + " operations on Windows");
    } else {
        // Only the following tests need refuser's connection backlog
        // to be saturated
        ExecutorService pumperExecutor = Executors.newSingleThreadExecutor(new ThreadFactory() {

            @Override
            public Thread newThread(Runnable r) {
                Thread t = new Thread(r);
                t.setDaemon(true);
                t.setName("Pumper");
                return t;
            }
        });
        pumpDone = false;
        try {
            Future<Integer> pumpFuture = pumpRefuser(pumperExecutor);
            waitPump("\nWait for initial Pump");
            test(socketChannelFactory, CONNECT);
            test(socketChannelFactory, FINISH_CONNECT);
            pumpDone = true;
            Integer newConn = pumpFuture.get(30, TimeUnit.SECONDS);
            log.println("Pump " + newConn + " connections.");
        } finally {
            pumperExecutor.shutdown();
        }
    }
    test(serverSocketChannelFactory, ACCEPT);
    test(datagramChannelFactory);
    test(pipeSourceChannelFactory);
    test(pipeSinkChannelFactory);
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) ExecutorService(java.util.concurrent.ExecutorService)

Example 69 with ThreadFactory

use of java.util.concurrent.ThreadFactory in project jdk8u_jdk by JetBrains.

the class Leaky method main.

public static void main(String[] args) throws Exception {
    ThreadFactory threadFactory = new ThreadFactory() {

        @Override
        public Thread newThread(Runnable r) {
            Thread t = new Thread(r);
            t.setDaemon(true);
            return t;
        }
    };
    AsynchronousChannelGroup group = AsynchronousChannelGroup.withFixedThreadPool(4, threadFactory);
    final int CONNECTION_COUNT = 10;
    Connection[] connections = new Connection[CONNECTION_COUNT];
    for (int i = 0; i < CONNECTION_COUNT; i++) {
        connections[i] = new Connection(group);
    }
    for (int i = 0; i < 1024; i++) {
        // initiate reads
        for (Connection conn : connections) {
            conn.startRead();
        }
        // write data so that the read can complete
        for (Connection conn : connections) {
            conn.write();
        }
        // complete read
        for (Connection conn : connections) {
            conn.finishRead();
        }
    }
    // print summary of buffer pool usage
    List<BufferPoolMXBean> pools = ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class);
    for (BufferPoolMXBean pool : pools) System.out.format("         %8s             ", pool.getName());
    System.out.println();
    for (int i = 0; i < pools.size(); i++) System.out.format("%6s %10s %10s  ", "Count", "Capacity", "Memory");
    System.out.println();
    for (BufferPoolMXBean pool : pools) {
        System.out.format("%6d %10d %10d  ", pool.getCount(), pool.getTotalCapacity(), pool.getMemoryUsed());
    }
    System.out.println();
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) BufferPoolMXBean(java.lang.management.BufferPoolMXBean)

Example 70 with ThreadFactory

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

the class ThreadFactoryBuilder method build.

private static ThreadFactory build(ThreadFactoryBuilder builder) {
    final String nameFormat = builder.nameFormat;
    final Boolean daemon = builder.daemon;
    final Integer priority = builder.priority;
    final UncaughtExceptionHandler uncaughtExceptionHandler = builder.uncaughtExceptionHandler;
    final ThreadFactory backingThreadFactory = (builder.backingThreadFactory != null) ? builder.backingThreadFactory : Executors.defaultThreadFactory();
    final AtomicLong count = (nameFormat != null) ? new AtomicLong(0) : null;
    return new ThreadFactory() {

        @Override
        public Thread newThread(Runnable runnable) {
            Thread thread = backingThreadFactory.newThread(runnable);
            if (nameFormat != null) {
                thread.setName(String.format(nameFormat, count.getAndIncrement()));
            }
            if (daemon != null) {
                thread.setDaemon(daemon);
            }
            if (priority != null) {
                thread.setPriority(priority);
            }
            if (uncaughtExceptionHandler != null) {
                thread.setUncaughtExceptionHandler(uncaughtExceptionHandler);
            }
            return thread;
        }
    };
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) AtomicLong(java.util.concurrent.atomic.AtomicLong) UncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler)

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