Do you really need to manually manipulate threads and iterators? You can use Java 8 Stream and let parallel() do the job.
By default, it will use one less thread since you have processors.
Example:
list.stream() .parallel() .forEach(this::doSomething) ;
Result:
49748, 13 49749, 13 49750, 13 192710, 14 105734, 17 105735, 17 105736, 17 [...]
Edit: if you are using maven, you will need to add this part of the configuration to pom.xml in order to use Java 8:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.3</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build>
source share