As already noted, a parallel thread blocks the current thread until all parallel tasks are complete. In fact, the current thread is usually also used to do some work, but if it finishes its part, then it waits for other threads (or steals part of their work to help them).
There is one special case: if a parallel thread operation throws an exception, then the processing of the thread in the main thread ends (exclusively), but other background threads may continue to process some input fragments. You can verify this using the following code:
// Create list of Strings "0", "1", "2", ..., "99" List<String> list = IntStream.range(0, 100).mapToObj(String::valueOf) .collect(Collectors.toCollection(ArrayList::new)); // replace one with non-numeric list.set(1, "foo"); // Convert every string to number and print it try { list.parallelStream().mapToInt(Integer::parseInt).forEach(System.out::println); } catch (NumberFormatException e) { // well some non-number encountered } System.out.println("Exited");
By running this code, you can sometimes see that some numbers are printed after the "Exited"
message.
source share