Why parallel streaming of entities list run on each entity more than one time?

Viewed 16

I have a java 17, spring boot project, and i'm trying to run a method on an entities list with parallel stream.

what happens is that the code run on each entity few times.

any idea how to make sure it's run only once for each entity?

thanks!

this is the streaming part:

    List<Entity> entityList = repository.findTop1000ByStatus(PENDING);
    
final var list = entityList;
ForkJoinPool customThreadPool = new ForkJoinPool(Math.min(entityList.size(), 100));
customThreadPool.submit(() -> list.stream().parallel().forEach(entity -> {
                            long userId = entity.getUserId();
                            UserDto user;
                            try {
    .
    .
    .

this is the gradle.build file:

buildscript {
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        classpath "com.avast.gradle:gradle-docker-compose-plugin:0.6.0"
    }
}

plugins {
    id 'io.franzbecker.gradle-lombok' version '5.0.0'
}
test {
    useJUnitPlatform()
}
lombok {
    version = lombokVersion
    sha256 = ""
}
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'docker-compose'

bootJar {
    baseName = "${baseName}##${version}"
    classifier = ""
    version = ""
}
task shadowJar(dependsOn: bootJar)
dependencies {
    implementation project(':base'),
            project(':queue'),
            project(':data-access'),
            project(':data-transfer'),
            project(':rest-services'),
            project(':cache'),
            project(':auth'),
            project(':common-services'),
            project(':vault')
    implementation("software.amazon.awssdk:s3:${awsSdk2Version}")
    implementation('org.springframework.boot:spring-boot-starter-json')
    implementation('org.springframework.boot:spring-boot-starter-data-jpa')
    implementation('org.springframework.boot:spring-boot-starter-validation')
    implementation('org.springframework.boot:spring-boot-starter-webflux')
    implementation("org.springframework.retry:spring-retry:${springRetryVersion}")
    implementation("com.opencsv:opencsv:${opencsvVersion}")
    implementation("com.google.code.findbugs:jsr305:${googleFindbugsVersion}")
    runtimeOnly('org.springframework.boot:spring-boot-devtools')
    compileOnly('org.springframework.boot:spring-boot-configuration-processor')
    testImplementation('org.springframework.boot:spring-boot-starter-test')
    testImplementation("com.h2database:h2:${H2DatabaseVersion}")
}

dockerCompose {
    removeContainers = true
}
0 Answers
Related