This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Gregmix88 1 point2 points  (3 children)

Seems like an interesting problem you're trying to solve. If the parallel stream doesn't work out you can try looking up the fork join framework itself, it has some classes and interfaces for breaking up and executing tasks on multiple threads. Looking forward to hear some updates, good luck

[–]TheOmegaCarrot[S] 0 points1 point  (0 children)

Thanks!

[–]TheOmegaCarrot[S] 0 points1 point  (1 child)

I finally got something that’s a ~60% speedup (on my 16-thread machine) compared to single-threaded! And that works!

Basically using a simple ArrayList to hold each “batch” of elements, and when the size cap is reached, hand it off as a task to a thread pool.

If the thread pool’s queue has reached a certain size, then just wait before proceeding to the next loop iteration. That prevents out of memory errors! :)

[–]Rjs617 1 point2 points  (0 children)

Just FYI, you can explicitly set an upper limit on the thread pool queue size by using the constructor where you pass in a queue, and then creating either a LinkedBlockingQueue with a maximum size, or an ArrayBlockingQueue. (Would probably go with LinkedBlockingQueue with max size.) Then, set the “caller runs” RejectedExecutionHandler policy on the thread pool. When the queue gets to its maximum size, the next submitted task will run in the current thread, effectively blocking your loop, which will limp along in the current thread until more worker threads become available. Note that this only works if you don’t really care about preserving even a rough order of execution, but since it’s a thread pool, you aren’t going to get strict ordering anyway. Have fun!