All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.yuyenews.concurrent.processing.map.ConcurrentMapAsync Maven / Gradle / Ivy

The newest version!
package com.github.yuyenews.concurrent.processing.map;

import java.util.Map;
import java.util.concurrent.*;

/**
 * 并发处理Map里的元素(异步)
 */
public class ConcurrentMapAsync {

    /**
     * 异步执行任务的线程池
     */
    private ThreadPoolExecutor poolExecutor;

    public ConcurrentMapAsync(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              ThreadFactory threadFactory) {

        poolExecutor = new ThreadPoolExecutor(corePoolSize,
                maximumPoolSize,
                keepAliveTime,
                unit,
                new LinkedBlockingQueue<>(),
                threadFactory);
    }

    public ConcurrentMapAsync(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit) {

        poolExecutor = new ThreadPoolExecutor(corePoolSize,
                maximumPoolSize,
                keepAliveTime,
                unit,
                new LinkedBlockingQueue<>());
    }

    /**
     * 并发执行器
     */
    private ConcurrentMapSync concurrentMapSync = new ConcurrentMapSync();

    /**
     * 将一个Map分成若干组,排队执行,每组内部并发执行
     * 同步执行
     * 默认分成10组,超时时间10分钟
     *
     * @param dataMap             数据集
     * @param concurrentMapRunner 执行器
     * @param 
     */
    public  ThreadPoolExecutor asyncRunner(Map dataMap,
                                   ConcurrentMapRunner concurrentMapRunner) {
        return asyncRunner(dataMap, concurrentMapRunner, 10, 10, TimeUnit.MINUTES);
    }

    /**
     * 将一个Map分成若干组,排队执行,每组内部并发执行
     * 同步执行
     * 默认超时时间10分钟
     *
     * @param dataMap             数据集
     * @param concurrentMapRunner 执行器
     * @param groupSize           每组大小,这个大小就决定了会同时开几个线程
     * @param 
     */
    public  ThreadPoolExecutor asyncRunner(Map dataMap,
                                   ConcurrentMapRunner concurrentMapRunner,
                                   int groupSize) {
        return asyncRunner(dataMap, concurrentMapRunner, groupSize, 10, TimeUnit.MINUTES);
    }

    /**
     * 将一个Map分成若干组,每一组分别用一个线程执行
     * 同步执行
     * 默认分成10组,超时时间10分钟
     *
     * @param dataMap                  数据集
     * @param concurrentMapGroupRunner 执行器
     * @param 
     */
    public  ThreadPoolExecutor asyncGroupRunner(Map dataMap,
                                        ConcurrentMapGroupRunner concurrentMapGroupRunner) {
        return asyncGroupRunner(dataMap, concurrentMapGroupRunner, 10, 10, TimeUnit.MINUTES);
    }

    /**
     * 将一个Map分成若干组,每一组分别用一个线程执行
     * 同步执行
     * 默认超时时间10分钟
     *
     * @param dataMap                  数据集
     * @param concurrentMapGroupRunner 执行器
     * @param groupSize                每组大小,这个大小就决定了会同时开几个线程
     * @param 
     */
    public  ThreadPoolExecutor asyncGroupRunner(Map dataMap,
                                        ConcurrentMapGroupRunner concurrentMapGroupRunner,
                                        int groupSize) {
        return asyncGroupRunner(dataMap, concurrentMapGroupRunner, groupSize, 10, TimeUnit.MINUTES);
    }

    /**
     * 将一个Map分成若干组,每一组分别用一个线程执行
     * 同步执行
     *
     * @param dataMap                  数据集
     * @param concurrentMapGroupRunner 执行器
     * @param groupSize                每组多少条,它关系到能分成多少组,而组数就决定了会同时开几个线程
     * @param timeout                  每一组的超时时间,单位由unit参数设置
     * @param unit                     超时时间单位
     * @param 
     */
    public  ThreadPoolExecutor asyncGroupRunner(Map dataMap,
                                        ConcurrentMapGroupRunner concurrentMapGroupRunner,
                                        int groupSize,
                                        long timeout,
                                        TimeUnit unit) {
        poolExecutor.submit(() -> {
            concurrentMapSync.syncGroupRunner(dataMap, concurrentMapGroupRunner, groupSize, timeout, unit);
        });

        return poolExecutor;
    }


    /**
     * 将一个Map分成若干组,排队执行,每组内部并发执行
     * 同步执行
     *
     * @param dataMap             数据集
     * @param concurrentMapRunner 执行器
     * @param groupSize           每组大小,这个大小就决定了会同时开几个线程
     * @param timeout             每一组的超时时间,单位由unit参数设置
     * @param unit                超时时间单位
     * @param 
     */
    public  ThreadPoolExecutor asyncRunner(Map dataMap,
                                   ConcurrentMapRunner concurrentMapRunner,
                                   int groupSize,
                                   long timeout,
                                   TimeUnit unit) {
        poolExecutor.submit(() -> {
            concurrentMapSync.syncRunner(dataMap, concurrentMapRunner, groupSize, timeout, unit);
        });

        return poolExecutor;
    }

    /**
     * 获取线程池
     *
     * @return
     */
    public ThreadPoolExecutor getPoolExecutor() {
        return poolExecutor;
    }

    /**
     * 关闭线程池
     */
    public void shutdown() {
        poolExecutor.shutdown();
    }

    /**
     * 立刻关闭线程池
     */
    public void shutdownNow() {
        poolExecutor.shutdownNow();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy