Java Parallel Streams closes a thread

I wrote a method using Java threads that just iterates over a list of objects and returns true / false, a certain condition is met

Java Method:

 boolean method(SampleObj sampleObj) {

   List testList = invokeSomeMethod();
   int result = testList
            .parallelStream()
            .filter(listObj -> (listObj.getAttr() = 1))
            .count(listObj -> listObj.isAttr4());

   return (result > 10);

 }

I wrote a Mock test case for the same. When I run the test case, the test succeeds, however I get a project design error of the project, indicating that all created threads have not been disabled.

I even tried using a stream with try-with resources so noo didn't help.

Mock Test:

@Test
public void testSomeMethod() {
    SampleObj sampleObj1 = new SampleObj(10, 20, 30, true);
    SampleObj sampleObj2 = new SampleObj(10, 20, 30, true);
    SampleObj sampleObj3 = new SampleObj(10, 20, 30, false);
    SampleObj sampleObjTest = new SampleObj(10, 20, 30, true);

    List<SampleObj> testList = new ArrayList<SampleObj>();
    testList.add(sampleObj1);
    testList.add(sampleObj2);
    testList.add(sampleObj3);

    when(mockedAttribute.invokeSomeMethod()).thenReturn(nodeList);

    ClassToBeTested classTest = createGenericMockRules();
    Assert.assertTrue(classTest.method(sampleObjTest));
}

PS I debugged to confirm that invokingSomeMethod () returns my mocked testList.

As far as I know, Java threads internally close the threads it creates. Am I implementing it wrong?

+6
1

Java- , , . ; , , ForkJoinPool :

commonPool() . ForkJoinTask, . ( ).

, , , "", . -, , , , , , . , , , , .

while(ForkJoinPool.commonPool().getPoolSize()>0)
    LockSupport.parkNanos(1000);

, . /. " ", , , .

, . - ..

+5

Source: https://habr.com/ru/post/1015353/


All Articles