So, I'm trying to get my head around the Stream API introduced in Java 8. I'm trying to make a stream that can run on a separate thread (just for educational purposes)
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 outcome is not what I would expect.. (I was expecting a crash, since I closed the input stream while the other thread is reading from it). Note the .parallel() method call. Instead the code seems to be executing 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's going on? Why is my code not crashing?
ForkJoinPoolif you don't want things to be blocked.