Does Stream.parallel () use the new stream?

So, I am trying to get the Stream API presented in Java 8. I am trying to create a stream that can work in a separate stream (for educational purposes only)

String oracle = "http://www.oracle.com"; URL url = new URL(oracle); BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); in.lines().parallel().forEach(System.out::println); System.out.print("CLOSING THE INPUT STREAM!, shouldnt this crash?"); in.close(); 

The result is not what I would expect .. (I was expecting a crash since I closed the input stream while another stream is reading from it). Pay attention to the .parallel() method call. Instead, the code seems to be executed in a sequential manner with no problems.

OUTPUT:

 <script language="JavaScript" src="http://www.oracleimg.com/us/assets/metrics/ora_ocom_hp.js"></script> <!-- End SiteCatalyst code --> <!-- SS_END_SNIPPET(fragment6,1)--> <!-- SS_BEGIN_SNIPPET(fragment7,ui)--> <!-- SS_END_SNIPPET(fragment7,ui)--> </html> CLOSING THE INPUT STREAM!, shouldnt this crash? 

Does anyone know what is going on? Why is my code not crashing?

+5
source share
3 answers

A parallel thread will actually try to split the work of reading lines into multiple threads. But the call itself is blocked, i.e. The operator waits until all threads have finished running, to move on to the next operator (where you close the input stream).

It should be noted that forEach does not guarantee that parallel actions are performed in the same order of the flow elements, so the printed lines in this case may not coincide with the original web page (see https://docs.oracle.com/javase/8/ docs / api / java / util / stream / Stream.html # forEach-java.util.function.Consumer- ).

+12
source

If you want to execute something in the background without directly blocking them to complete, you can use java.util.concurrent.CompletableFuture.runAsync(Runnable) and its associated methods. It returns a CompletableFuture, which can be joined later if necessary.

+2
source

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.

+1
source

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


All Articles