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

com.feilong.core.lang.thread.PartitionThreadExecutor Maven / Gradle / Ivy

Go to download

feilong is a suite of core and expanded libraries that include utility classes, http, excel,cvs, io classes, and much much more.

There is a newer version: 4.0.8
Show newest version
/*
 * Copyright (C) 2008 feilong
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.feilong.core.lang.thread;

import java.util.List;
import java.util.Map;

/**
 * 分区线程执行器.
 * 
 * 

背景:

* *
* *

* 经常有如此的需求, 下载客户8000张图片加工处理, 如果串行执行耗时很长,此时需要使用多线程,
* 但是如果从头开始写会开发蛮久时间,此时把核心功能进行封装 ,你只需要传入 8000和url list, 告知每个线程处理多少张图片, 以及每个线程的代码处理方式即可,实现类会自动分区创建线程并执行 *

* *

* 契合 feilong 的思想, Reduce development, Release ideas (减少开发,释放思想) *

*
* *

快速调用以及实现类:

* *
*

* 目前已知快速调用有 {@link com.feilong.core.lang.ThreadUtil#execute(List, int, Map, PartitionRunnableBuilder) }, 或者调用 * {@link com.feilong.core.lang.thread.DefaultPartitionThreadExecutor}, 或者调用 * com.feilong.spring.scheduling.concurrent.AsyncTaskExecutorPartitionThreadExecutor *

* *
* *

如果使用spring asyncTaskExecutor:

*
* *
{@code
 
    
         
        
         
        
         
        
         
        
    
    
    

    
        
    
    
    }
 * 
* * 然后 java 代码调用 * *
 * 
 * @Autowired
 * private PartitionThreadExecutor partitionThreadExecutor;
 * 
 * @Test
 * public void test(){
 *     final List{@code } list = toList(1, 2, 3, 4, 5);
 * 
 *     partitionThreadExecutor.execute(list, 2, new PartitionRunnableBuilder{@code }(){
 * 
 *         public Runnable build(List{@code } perBatchList,final PartitionThreadEntity partitionThreadEntity,Map{@code } paramsMap){
 *             return new Runnable(){
 * 
 *                 public void run(){
 *                     for (Integer integer : list){
 *                         LOGGER.debug("{}-{}", partitionThreadEntity.getBatchNumber(), integer);
 *                     }
 *                 }
 *             };
 *         }
 *     });
 * }
 * 
* *
* * @author feilong * @since 1.11.0 * @since 2.0.0 move from package com.feilong.core.lang */ public interface PartitionThreadExecutor{ /** * 给定一个待解析的 list,设定每个线程执行多少条 eachSize ,使用自定义的 * partitionRunnableBuilder,自动构造多条线程并运行. * *

* 如果 list 是null,抛出 {@link NullPointerException}
* 如果 list 是empty,抛出 {@link IllegalArgumentException}
* 如果 {@code eachSize <=0} ,抛出 {@link IllegalArgumentException}
* 如果 partitionRunnableBuilder 是null,抛出 {@link NullPointerException}
*

* * @param * the generic type * @param list * 执行解析的list * *

* 比如 100000个 User,不能为null或者empty *

* @param eachSize * 每个线程执行多少个对象 * *

* 比如 一个线程解析 1000个 User, 那么程序内部 会自动创建 100000/1000个 线程去解析;
* 必须{@code >}0 *

* @param partitionRunnableBuilder * 每个线程做的事情,不能为null * @see com.feilong.core.lang.ThreadUtil#execute(List, int, Map, PartitionRunnableBuilder) * @since 2.0.0 change name from excute to execute */ void execute(List list,int eachSize,PartitionRunnableBuilder partitionRunnableBuilder); //--------------------------------------------------------------- /** * 给定一个待解析的 list,设定每个线程执行多少条 eachSize,传入一些额外的参数 paramsMap,使用自定义的 * partitionRunnableBuilder,自动构造多条线程并运行. * *

* 如果 list 是null,抛出 {@link NullPointerException}
* 如果 list 是empty,抛出 {@link IllegalArgumentException}
* 如果 {@code eachSize <=0} ,抛出 {@link IllegalArgumentException}
* 如果 partitionRunnableBuilder 是null,抛出 {@link NullPointerException}
*

* * @param * the generic type * @param list * 执行解析的list * *

* 比如 100000个 User,不能为null或者empty *

* @param eachSize * 每个线程执行多少个对象 * *

* 比如 一个线程解析 1000个 User, 那么程序内部 会自动创建 100000/1000个 线程去解析;
* 必须{@code >}0 *

* @param paramsMap * 自定义的相关参数 * *

* 该参数目的是你可以在自定义的 partitionRunnableBuilder中使用;
* 如果你传入的partitionRunnableBuilder中不需要额外的自定义参数,那么此处可以传入null *

* @param partitionRunnableBuilder * 每个线程做的事情,不能为null * @see com.feilong.core.lang.ThreadUtil#execute(List, int, Map, PartitionRunnableBuilder) * @since 2.0.0 change name from excute to execute */ void execute(List list,int eachSize,Map paramsMap,PartitionRunnableBuilder partitionRunnableBuilder); }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy