Example usage for java.util.concurrent ThreadPoolExecutor ThreadPoolExecutor

List of usage examples for java.util.concurrent ThreadPoolExecutor ThreadPoolExecutor

Introduction

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

Prototype

public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit,
        BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) 

Source Link

Document

Creates a new ThreadPoolExecutor with the given initial parameters.

Usage

From source file:Main.java

public static ThreadPoolExecutor newFixedThreadPool(int corePoolSize, BlockingQueue<Runnable> queue,
        ThreadFactory threadFactory, RejectedExecutionHandler handler) {
    return new ThreadPoolExecutor(corePoolSize, corePoolSize, 0, TimeUnit.MILLISECONDS, queue, threadFactory,
            handler);/*w  w w  .ja v  a 2  s.  c  o m*/
}

From source file:com.aliyun.oss.common.comm.TimeoutServiceClient.java

public TimeoutServiceClient(ClientConfiguration config) {
    super(config);

    int processors = Runtime.getRuntime().availableProcessors();
    executor = new ThreadPoolExecutor(processors * 5, processors * 10, 60L, TimeUnit.SECONDS,
            new ArrayBlockingQueue<Runnable>(processors * 100), Executors.defaultThreadFactory(),
            new ThreadPoolExecutor.CallerRunsPolicy());
    executor.allowCoreThreadTimeOut(true);
}

From source file:org.apache.hadoop.hbase.ipc.FifoRpcScheduler.java

@Override
public void start() {
    this.executor = new ThreadPoolExecutor(handlerCount, handlerCount, 60, TimeUnit.SECONDS,
            new ArrayBlockingQueue<Runnable>(maxQueueLength),
            new DaemonThreadFactory("FifoRpcScheduler.handler"), new ThreadPoolExecutor.CallerRunsPolicy());
}

From source file:com.alibaba.otter.shared.communication.core.impl.DefaultCommunicationClientImpl.java

public void initial() {
    RejectedExecutionHandler handler = null;
    if (discard) {
        handler = new ThreadPoolExecutor.DiscardPolicy();
    } else {/* w ww .  ja v  a 2  s .c  o  m*/
        handler = new ThreadPoolExecutor.AbortPolicy();
    }

    executor = new ThreadPoolExecutor(poolSize, poolSize, 60 * 1000L, TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<Runnable>(10 * 1000), new NamedThreadFactory("communication-async"),
            handler);
}

From source file:com.alibaba.napoli.metamorphosis.client.consumer.RecoverStorageManager.java

public RecoverStorageManager(final MetaClientConfig metaClientConfig,
        final SubscribeInfoManager subscribeInfoManager) {
    super();/*from w  w w.  j  a va 2 s .c  om*/

    // caller run
    this.threadPoolExecutor = new ThreadPoolExecutor(metaClientConfig.getRecoverThreadCount(),
            metaClientConfig.getRecoverThreadCount(), 60, TimeUnit.SECONDS,
            new ArrayBlockingQueue<Runnable>(100), new NamedThreadFactory("Recover-thread"),
            new ThreadPoolExecutor.CallerRunsPolicy());
    // this.startRecover(metaClientConfig);
    this.makeDataDir();
    this.subscribeInfoManager = subscribeInfoManager;
    this.loadStores();
}

From source file:org.opendaylight.controller.sal.binding.codegen.impl.SingletonHolder.java

/**
 * @deprecated This method is only used from configuration modules and thus callers of it
 *             should use service injection to make the executor configurable.
 *//*from w  w w .j av  a2s. c om*/
@Deprecated
public static synchronized ListeningExecutorService getDefaultNotificationExecutor() {

    if (NOTIFICATION_EXECUTOR == null) {
        int queueSize = MAX_NOTIFICATION_QUEUE_SIZE;
        final String queueValue = System.getProperty(NOTIFICATION_QUEUE_SIZE_PROPERTY);
        if (StringUtils.isNotBlank(queueValue)) {
            try {
                queueSize = Integer.parseInt(queueValue);
                logger.trace("Queue size was set to {}", queueSize);
            } catch (final NumberFormatException e) {
                logger.warn("Cannot parse {} as set by {}, using default {}", queueValue,
                        NOTIFICATION_QUEUE_SIZE_PROPERTY, queueSize);
            }
        }

        // Overriding the queue:
        // ThreadPoolExecutor would not create new threads if the queue is not full, thus adding
        // occurs in RejectedExecutionHandler.
        // This impl saturates threadpool first, then queue. When both are full caller will get blocked.
        final BlockingQueue<Runnable> delegate = new LinkedBlockingQueue<>(queueSize);
        final BlockingQueue<Runnable> queue = new ForwardingBlockingQueue<Runnable>() {
            @Override
            protected BlockingQueue<Runnable> delegate() {
                return delegate;
            }

            @Override
            public boolean offer(final Runnable r) {
                // ThreadPoolExecutor will spawn a new thread after core size is reached only
                // if the queue.offer returns false.
                return false;
            }
        };

        final ThreadFactory factory = new ThreadFactoryBuilder().setDaemon(true)
                .setNameFormat("md-sal-binding-notification-%d").build();

        final ThreadPoolExecutor executor = new ThreadPoolExecutor(CORE_NOTIFICATION_THREADS,
                MAX_NOTIFICATION_THREADS, NOTIFICATION_THREAD_LIFE, TimeUnit.SECONDS, queue, factory,
                new RejectedExecutionHandler() {
                    // if the max threads are met, then it will raise a rejectedExecution. We then push to the queue.
                    @Override
                    public void rejectedExecution(final Runnable r, final ThreadPoolExecutor executor) {
                        try {
                            executor.getQueue().put(r);
                        } catch (final InterruptedException e) {
                            throw new RejectedExecutionException("Interrupted while waiting on the queue", e);
                        }
                    }
                });

        NOTIFICATION_EXECUTOR = MoreExecutors.listeningDecorator(executor);
    }

    return NOTIFICATION_EXECUTOR;
}

From source file:com.haulmont.cuba.core.app.ClusterManager.java

@PostConstruct
protected void init() {
    int nThreads = clusterConfig.getClusterMessageSendingThreadPoolSize();
    executor = new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<>(clusterConfig.getClusterMessageSendingQueueCapacity()),
            new ThreadFactoryBuilder().setNameFormat("ClusterManagerMessageSender-%d").build(),
            new RejectedExecutionHandler() {
                @Override//from   ww  w.j a v  a2  s . c  om
                public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                    SendMessageRunnable sendMessageRunnable = (SendMessageRunnable) r;
                    log.info("Queue capacity is exceeded. Message: {}: {}",
                            sendMessageRunnable.message.getClass(), sendMessageRunnable.message);
                }
            });
}

From source file:com.alibaba.otter.node.etl.common.db.DbPerfIntergration.java

@Test
public void test_stack() {
    DbMediaSource dbMediaSource = new DbMediaSource();
    dbMediaSource.setId(1L);//from  w ww .  j  av a2s . c om
    dbMediaSource.setDriver("com.mysql.jdbc.Driver");
    dbMediaSource.setUsername("otter");
    dbMediaSource.setPassword("otter");
    dbMediaSource.setUrl("jdbc:mysql://127.0.0.1:3306/retl");
    dbMediaSource.setEncode("UTF-8");
    dbMediaSource.setType(DataMediaType.MYSQL);

    DbDataMedia dataMedia = new DbDataMedia();
    dataMedia.setSource(dbMediaSource);
    dataMedia.setId(1L);
    dataMedia.setName("ljhtable1");
    dataMedia.setNamespace("otter");

    final DbDialect dbDialect = dbDialectFactory.getDbDialect(2L, dataMedia.getSource());
    want.object(dbDialect).clazIs(MysqlDialect.class);

    final TransactionTemplate transactionTemplate = dbDialect.getTransactionTemplate();

    // ??
    int minute = 5;
    int nextId = 1;
    final int thread = 10;
    final int batch = 50;
    final String sql = "insert into otter.ljhtable1 values(? , ? , ? , ?)";

    final CountDownLatch latch = new CountDownLatch(thread);
    ExecutorService executor = new ThreadPoolExecutor(thread, thread, 60, TimeUnit.SECONDS,
            new ArrayBlockingQueue(thread * 2), new NamedThreadFactory("load"),
            new ThreadPoolExecutor.CallerRunsPolicy());

    for (int sec = 0; sec < minute * 60; sec++) {
        // 
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < thread; i++) {
            final int start = nextId + i * batch;
            executor.submit(new Runnable() {

                public void run() {
                    try {
                        transactionTemplate.execute(new TransactionCallback() {

                            public Object doInTransaction(TransactionStatus status) {
                                JdbcTemplate jdbcTemplate = dbDialect.getJdbcTemplate();
                                return jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {

                                    public void setValues(PreparedStatement ps, int idx) throws SQLException {
                                        int id = start + idx;
                                        StatementCreatorUtils.setParameterValue(ps, 1, Types.INTEGER, null, id);
                                        StatementCreatorUtils.setParameterValue(ps, 2, Types.VARCHAR, null,
                                                RandomStringUtils.randomAlphabetic(1000));
                                        // RandomStringUtils.randomAlphabetic()
                                        long time = new Date().getTime();
                                        StatementCreatorUtils.setParameterValue(ps, 3, Types.TIMESTAMP,
                                                new Timestamp(time));
                                        StatementCreatorUtils.setParameterValue(ps, 4, Types.TIMESTAMP,
                                                new Timestamp(time));
                                    }

                                    public int getBatchSize() {
                                        return batch;
                                    }
                                });
                            }
                        });
                    } finally {
                        latch.countDown();
                    }
                }
            });

        }

        long endTime = System.currentTimeMillis();
        try {
            latch.await(1000 * 60L - (endTime - startTime), TimeUnit.MILLISECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        if (latch.getCount() != 0) {
            System.out.println("perf is not enough!");
            System.exit(-1);
        }
        endTime = System.currentTimeMillis();
        System.out.println("Time cost : " + (System.currentTimeMillis() - startTime));
        try {
            TimeUnit.MILLISECONDS.sleep(1000L - (endTime - startTime));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        nextId = nextId + thread * batch;
    }
    executor.shutdown();
}

From source file:org.addsimplicity.anicetus.io.jms.JMSDeliveryAdapter.java

/**
 * Called by Spring once all properties have been set. This method will
 * establish the connection to the JMS broker.
 *//*w  w w. j a  v  a 2  s  . c  om*/
public void afterPropertiesSet() throws Exception {
    BlockingQueue<Runnable> q = new LinkedBlockingQueue<Runnable>(m_maxDeliveryQueue);
    m_executor = new ThreadPoolExecutor(1, m_maxDeliveryThreads, 60l, TimeUnit.SECONDS, q,
            new DeliveryThreadFactory(), m_rejectionHandler);

    m_template = new JmsTemplate(m_connectionFactory);
    m_template.setDefaultDestination(m_destination);
    m_template.setMessageConverter(m_messageConverter);
}

From source file:com.turn.ttorrent.common.TorrentCreator.java

/**
 * Creates a new executor suitable for torrent hashing.
 * /*from   www  .j av a2  s .  c o m*/
 * This executor controls memory usage by using a bounded queue, and the
 * CallerRunsPolicy slows down the producer if the queue bound is exceeded.
 * The requirement is then to make the queue large enough to keep all the
 * executor threads busy if the producer executes a task itself.
 * 
 * In terms of memory, Executor.execute is much more efficient than
 * ExecutorService.submit, and ByteBuffer(s) released by the ChunkHasher(s)
 * remain in eden space, so are rapidly recycled for reading the next
 * block(s). JVM ergonomics can make this much more efficient than any
 * heap-based strategy we might devise. Now, we want the queue size small
 * enough that JVM ergonomics keeps the eden size small.
 */
@Nonnull
public static ThreadPoolExecutor newExecutor(@Nonnull String peerName) {
    int threads = getHashingThreadsCount();
    logger.info("Creating ExecutorService with {} threads", new Object[] { threads });
    ThreadFactory factory = new DefaultThreadFactory("bittorrent-executor-" + peerName, true);
    ThreadPoolExecutor service = new ThreadPoolExecutor(0, threads, 1L, TimeUnit.SECONDS,
            new ArrayBlockingQueue<Runnable>(threads * 3), factory, new ThreadPoolExecutor.CallerRunsPolicy());
    service.allowCoreThreadTimeOut(true);
    return service;
}