자바 는 다 중 스 레 드 비동기 로 대량 업데이트 작업 을 수행 합 니 다.
많은 개발 자 들 이 프로젝트 가 데 이 터 를 대량으로 조작 할 때 많은 고민 을 할 것 이 라 고 믿 습 니 다.특히 데이터 의 양 이 많은 상황 에서 효율 문 제 는 바로 도마 에 올 렸 습 니 다.따라서 다 중 스 레 드 를 열 어 대량 임 무 를 수행 하 는 것 은 매우 중요 한 대량 작업 사고 이다.사실은 이런 사고 가 실현 되 는 것 도 매우 간단 하 다.대량으로 업 데 이 트 된 작업 예 를 들 어:
전체 흐름 도
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");
}
}
마지막 에 쓰다다 중 스 레 드 는 자바 의 어 려 운 점 이지 만 그것 도 재 미 있 습 니 다.달 아 나 는 사람 이 인생 에서 다 중 스 레 드 모드 를 열 었 다 고 들 었 습 니 다.
자바 가 다 중 스 레 드 비동기 로 대량 업데이트 작업 을 수행 하 는 것 에 관 한 이 글 은 여기까지 소개 되 었 습 니 다.더 많은 자바 다 중 스 레 드 비동기 로 대량 업데이트 내용 을 수행 하 는 것 은 우리 의 이전 글 을 검색 하거나 아래 의 관련 글 을 계속 조회 하 시기 바 랍 니 다.앞으로 많은 지원 바 랍 니 다!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.