... In order to do that, I have to make sure that my sequential BFS works perfectly. ...
Have you tried writing some unit-tests to prove or disprove that the function? On the whole it looks alright to me though it really depends on what the
visitor.visit(...) does. Also, not clear what
Minor comments :
q.forEach(currNode ->nextNodes.addAll(outgoingNeighbors(currNode))); is missing a space
for (int child : nextNodes)... can use a forEach as well.
- If you dequeue instead of doing the forEach, you should not need to do the clear at the end.
... I should I apply threads or executor service to run it in parallel.
When using multi-threading, my advice is always to break things into non-dependent tasks. Currently the "visitor" object seems a bit suspect and possible cause of future troubles.
Do you have any code that does the multi-threading?
Will my BFS break if the graph is too big?
Sure. If you just mean can you call something with a 1000 nodes, should be fine. If you plan on creating a million threads on a chromebook, you may have some issues. It really depends on your definition of big and the limits you're working with.
Note that often issues of scale are solved by optimizing the code/solution or by solving problems in a distributed fashion. If you start hitting such problems, I'd recommend start with a debugger, figure out what's wrong and maybe then start working out a good solution for whatever issue you discover.