elastic-job-thread-pool

在这里插入图片描述
本图来自https://shardingsphere.apache.org/elasticjob/current/cn/dev-manual/thread-pool/

JobExecutorServiceHandler

public interface JobExecutorServiceHandler extends TypedSPI {
    
    /**
     * Create executor service.
     * 
     * @param jobName job name
     * 
     * @return executor service
     */
    ExecutorService createExecutorService(String jobName);
}

实现图

在这里插入图片描述

ElasticJobExecutorService

其实就是1个ThreadPoolExecutor

public final class ElasticJobExecutorService {
    
    private final ThreadPoolExecutor threadPoolExecutor;
    
    private final BlockingQueue<Runnable> workQueue;
    
    public ElasticJobExecutorService(final String namingPattern, final int threadSize) {
        workQueue = new LinkedBlockingQueue<>();
        threadPoolExecutor = new ThreadPoolExecutor(
                threadSize, threadSize, 5L, TimeUnit.MINUTES, workQueue, new BasicThreadFactory.Builder().namingPattern(String.join("-", namingPattern, "%s")).build());
        threadPoolExecutor.allowCoreThreadTimeOut(true);
    }

}
    

AbstractJobExecutorServiceHandler

子类要实现getPoolSize()方法,这个方法返回值就是java线程池的corePoolSize、maximumPoolSize

/**
 * Abstract job executor service handler.
 **/
public abstract class AbstractJobExecutorServiceHandler implements JobExecutorServiceHandler {
    
    @Override
    public ExecutorService createExecutorService(final String jobName) {
        return new ElasticJobExecutorService("elasticjob-" + jobName, getPoolSize()).createExecutorService();
    }
    
    protected abstract int getPoolSize();
}

SingleThreadJobExecutorServiceHandler

corePoolSize=1, maximumPoolSize=1

public final class SingleThreadJobExecutorServiceHandler extends AbstractJobExecutorServiceHandler {
    
    @Override
    protected int getPoolSize() {
        return 1; // threadSize= corePoolSize=1,  maximumPoolSize=1
    }
    
    @Override
    public String getType() {
        return "SINGLE_THREAD";
    }
}

CPUUsageJobExecutorServiceHandler

corePoolSize = Runtime.getRuntime().availableProcessors() * 2
maximumPoolSize = corePoolSize

/**
 * Job executor service handler with use CPU available processors.
 */
public final class CPUUsageJobExecutorServiceHandler extends AbstractJobExecutorServiceHandler {
    
    @Override
    protected int getPoolSize() {
        return Runtime.getRuntime().availableProcessors() * 2;
    }
    
    @Override
    public String getType() {
        return "CPU";
    }
}

JobExecutorServiceHandlerFactory

public final class JobExecutorServiceHandlerFactory {
    
    private static final Map<String, JobExecutorServiceHandler> HANDLERS = new LinkedHashMap<>();
    
    private static final String DEFAULT_HANDLER = "CPU";
    
    static {
        for (JobExecutorServiceHandler each : ServiceLoader.load(JobExecutorServiceHandler.class)) {
            HANDLERS.put(each.getType(), each);
        }
    }
    
    /**
     * Get job executor service handler.
     *
     * @param type executor service handler type
     * @return executor service handler
     */
    public static JobExecutorServiceHandler getHandler(final String type) {
        if (Strings.isNullOrEmpty(type)) {
            return HANDLERS.get(DEFAULT_HANDLER);
        }
        if (!HANDLERS.containsKey(type)) {
            throw new JobConfigurationException("Can not find executor service handler type '%s'.", type);
        }
        return HANDLERS.get(type);
    }
}

spi

org.apache.shardingsphere.elasticjob.infra.handler.threadpool.JobExecutorServiceHandler

org.apache.shardingsphere.elasticjob.infra.handler.threadpool.impl.CPUUsageJobExecutorServiceHandler
org.apache.shardingsphere.elasticjob.infra.handler.threadpool.impl.SingleThreadJobExecutorServiceHandler
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值