Skip to content

Commit

Permalink
refactor: 싱글 스레드로 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
drunkenhw committed Oct 19, 2023
1 parent 32ec49d commit aa498d0
Showing 1 changed file with 6 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,43 +14,30 @@
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Slf4j
@RequiredArgsConstructor
@Service
public class StationGridFacadeService {

private static final int BATCH_SIZE = 1000;

private final StationGridService stationGridService;
private final StationQueryService stationQueryService;
private final StationGridCacheService stationGridCacheService;
private final ExecutorService executor = Executors.newFixedThreadPool(5);

public List<GridWithCount> createGridWithCounts() {
GridGenerator gridGenerator = new GridGenerator();
List<Grid> grids = gridGenerator.createKorea();
List<CompletableFuture<Void>> futures = new ArrayList<>();

long loopSize = stationQueryService.findStationCount() / BATCH_SIZE + 1;

int size = 1000;
int page = 0;
for (int i = 0; i < loopSize; i++) {
int finalPage = page;
CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
List<StationPoint> stationPoints = stationQueryService.findStationPoint(finalPage, BATCH_SIZE);
stationGridService.assignStationGrids(grids, stationPoints);
}, executor);
futures.add(future);
while (size == 1000) {
log.info("page : {}", page);
List<StationPoint> stationPoint = stationQueryService.findStationPoint(page, size);
stationGridService.assignStationGrids(grids, stationPoint);
page++;
size = stationPoint.size();
}

futures.forEach(CompletableFuture::join);
executor.shutdown();

List<GridWithCount> list = createRandomPointWhereHasStation(grids);
return new ArrayList<>(list);
}
Expand Down

0 comments on commit aa498d0

Please sign in to comment.