자바 는 다 중 스 레 드 비동기 로 대량 업데이트 작업 을 수행 합 니 다.

앞 에 쓰기:
많은 개발 자 들 이 프로젝트 가 데 이 터 를 대량으로 조작 할 때 많은 고민 을 할 것 이 라 고 믿 습 니 다.특히 데이터 의 양 이 많은 상황 에서 효율 문 제 는 바로 도마 에 올 렸 습 니 다.따라서 다 중 스 레 드 를 열 어 대량 임 무 를 수행 하 는 것 은 매우 중요 한 대량 작업 사고 이다.사실은 이런 사고 가 실현 되 는 것 도 매우 간단 하 다.대량으로 업 데 이 트 된 작업 예 를 들 어:
전체 흐름 도
在这里插入图片描述
  • 절 차 는 대량 업데이트 가 필요 한 대 집합 A 를 획득 하고 대 집합 을 분리 하여 N 개의 작은 집합 A-1~A-N 으로 나눈다.
  • 스 레 드 탱크 를 열 고 집합 크기 에 대해 조 참 을 하고 작은 집합 에 대해 대량으로 업데이트 작업 을 한다.
  • 절 차 를 제어 하고 스 레 드 의 집행 순 서 를 제어 한다.지 정 된 크기 에 따라 집합 하 는 도구 클래스
  • 
    import com.google.common.collect.Lists;
    import org.apache.commons.collections.CollectionUtils;
    
    import java.util.List;
    
    /**
     *        
     *
     * @author shiwen
     * @date 2020/12/27
     */
    public class SplitListUtils {
    
     /**
      *     
      *
      * @param <T>     
      * @param resList        
      * @param subListLength           
      * @return                
      *        guava common      
      **/
     public static <T> List<List<T>> split(List<T> resList, int subListLength) {
      if (CollectionUtils.isEmpty(resList) || subListLength <= 0) {
       return Lists.newArrayList();
      }
      List<List<T>> ret = Lists.newArrayList();
      int size = resList.size();
      if (size <= subListLength) {
       //       subListLength      
       ret.add(resList);
      } else {
       int pre = size / subListLength;
       int last = size % subListLength;
       //   pre   ,       subListLength    
       for (int i = 0; i < pre; i++) {
        List<T> itemList = Lists.newArrayList();
        for (int j = 0; j < subListLength; j++) {
         itemList.add(resList.get(i * subListLength + j));
        }
        ret.add(itemList);
       }
       // last     
       if (last > 0) {
        List<T> itemList = Lists.newArrayList();
        for (int i = 0; i < last; i++) {
         itemList.add(resList.get(pre * subListLength + i));
        }
        ret.add(itemList);
       }
      }
      return ret;
     }
    
     //     
     public static void main(String[] args) {
      List<String> list = Lists.newArrayList();
      int size = 1099;
      for (int i = 0; i < size; i++) {
       list.add("hello-" + i);
      }
      //             
      List<List<String>> temps = split(list, 100);
      int j = 0;
      //                  
      for (List<String> obj : temps) {
       System.out.println(String.format("row:%s -> size:%s,data:%s", ++j, obj.size(), obj));
      }
     }
    
    }
    비동기 작업 을 수행 하 는 스 레 드 풀 열기
    
    public void threadMethod() {
     List<T> updateList = new ArrayList();
     //       ,              !!!!
     ThreadPoolExecutor threadPool = new ThreadPoolExecutor(20, 50,
       4, TimeUnit.SECONDS, new ArrayBlockingQueue(10), new ThreadPoolExecutor.AbortPolicy());
     //       N    ,      size       (    100   ),           ,          
     List<T> splitNList = SplitListUtils.split(totalList, 100);
     //            
     CountDownLatch countDownLatch = new CountDownLatch(splitNList.size());
     //             ,       ,       
     for (List<T> singleList : splitNList) {
      //      
      threadPool.execute(new Thread(new Runnable(){
       @Override
       public void run() {
        for (Entity yangshiwen : singleList) {
         //             ,                list
         // ......
         
         //      - 1,    0   await()
         countDownLatch.countDown();
        }
       }
      }));
     }
     try {
      //            ,         
      countDownLatch.await();
     } catch (InterruptedException e) {
      throw new BusinessLogException(ResponseEnum.FAIL);
     }
     //   mybatis                ,          
     if (GeneralUtil.listNotNull(updateList)) {
      batchUpdateEntity(updateList);
      LogUtil.info("xxxxxxxxxxxxxxx");
     }
    }
    마지막 에 쓰다
    다 중 스 레 드 는 자바 의 어 려 운 점 이지 만 그것 도 재 미 있 습 니 다.달 아 나 는 사람 이 인생 에서 다 중 스 레 드 모드 를 열 었 다 고 들 었 습 니 다.
    자바 가 다 중 스 레 드 비동기 로 대량 업데이트 작업 을 수행 하 는 것 에 관 한 이 글 은 여기까지 소개 되 었 습 니 다.더 많은 자바 다 중 스 레 드 비동기 로 대량 업데이트 내용 을 수행 하 는 것 은 우리 의 이전 글 을 검색 하거나 아래 의 관련 글 을 계속 조회 하 시기 바 랍 니 다.앞으로 많은 지원 바 랍 니 다!

    좋은 웹페이지 즐겨찾기