Page 1 of 1

enthuware.ocpjp.v8.2.1842 (1z0-813 / 02-Concurrency)

Posted: Sun Mar 19, 2017 6:17 pm
by foxxbl
Hi!
For 1z0-813 (upgrade Java 1.6 or earlier to 1.8), in the Objective-wise tests on 02-Concurrency, included is enthuware.ocpjp.v8.2.1842 exam question which should be IMO part of the Java Streams (07-Stream API) Objective-wise tests, as specified by the 1z0-813 exam topics.

Please clarify it, thanks!
Cheers,
Boris

Re: enthuware.ocpjp.v8.2.1842 (1z0-813 / 02-Concurrency)

Posted: Mon Mar 20, 2017 2:09 am
by admin
It could be in both because of the usage of AtomicInteger.

Re: enthuware.ocpjp.v8.2.1842 (1z0-813 / 02-Concurrency)

Posted: Wed Dec 05, 2018 1:54 pm
by ramon.carrascom
Hi!
I've run this code, and I can't understand one thing: I thought "filter" intermediate operation had to check all elements in stream to see if each one passes or not. But, running this code, and adding a System.out.println(e) in the filter, I've seen that not all elements are being checked. How is this possible? Also considering that the terminal operation is "allMatch", so it needs to traverse all elements to see if all of them are matching its own condition. Could you please help me? Thanks in advance.

Re: enthuware.ocpjp.v8.2.1842 (1z0-813 / 02-Concurrency)

Posted: Wed Dec 05, 2018 5:31 pm
by admin
There are effectively two filter operations happening in the given code - filter and allmatch. Are you printing msgs from both?

Please post the exact code that you are running.

Re: enthuware.ocpjp.v8.2.1842 (1z0-813 / 02-Concurrency)

Posted: Thu Dec 06, 2018 3:03 am
by ramon.carrascom
Of course! Here it is:

Code: Select all

AtomicInteger ai = new AtomicInteger();
		Stream<String> stream = Stream.of("old", "king", "cole", "was", "a", "merry", "old", "soul").parallel();
		stream.filter(e -> {
			System.out.println("In filter: " + e);
			ai.incrementAndGet();
			return e.contains("o");
		}).allMatch(x -> {
			System.out.println("In allMatch: " + x);
			return x.indexOf("o") > 0;
		});
		System.out.println("AI = " + ai);
And the output:
In filter: merry
In filter: cole
In filter: king
In filter: soul
In filter: old
In allMatch: old
In allMatch: cole
In filter: a
In allMatch: soul
AI = 6
I perfectly understand the output from "In allMatch", but... why doesn't "In filter" show all the 8 elements? I mean... I could understand that all of them would be shown in unordered manner, but all of them. In this output I'm missing another "old", cause there is no "distinct" intermediate operation before filter... I'm really confused!! :?

Re: enthuware.ocpjp.v8.2.1842 (1z0-813 / 02-Concurrency)

Posted: Thu Dec 06, 2018 9:50 am
by admin
Oh ok, I understand your question now. If you look at the description of allMatch, it clearly says:
Returns whether all elements of this stream match the provided predicate. May not evaluate the predicate on all elements if not necessary for determining the result. If the stream is empty then true is returned and the predicate is not evaluated.
So, as soon as the condition evaluates to false for an element, the stream processing is stopped. The reason why all elements are not tested in the filter invocation is also given in the API description of Stream. It says, "Streams are lazy; computation on the source data is only performed when the terminal operation is initiated, and source elements are consumed only as needed."

Re: enthuware.ocpjp.v8.2.1842 (1z0-813 / 02-Concurrency)

Posted: Thu Dec 06, 2018 12:32 pm
by ramon.carrascom
Now I see!! I was confused thinking that the culprit was the concurrency, when it really was the allMatch. Thank you so much!!

Re: enthuware.ocpjp.v8.2.1842 (1z0-813 / 02-Concurrency)

Posted: Tue Jul 21, 2020 3:31 am
by moostebring
Hello,

Could you explain why it's always 4 when you turn this into a for(;;) loop? I wanted to see if it's always 'random' by putting a for loop around it and call it x amount of times. When it's ran 500+ times you start seeing only 4's.

Re: enthuware.ocpjp.v8.2.1842 (1z0-813 / 02-Concurrency)

Posted: Tue Jul 21, 2020 3:38 am
by admin
Please post the exact code that you ran.

Re: enthuware.ocpjp.v8.2.1842 (1z0-813 / 02-Concurrency)

Posted: Tue Jul 21, 2020 9:49 am
by moostebring

Code: Select all

public static void main(String[] args) throws Exception {
        for(int i = 0; i < 100000; i ++) {
            AtomicInteger ai = new AtomicInteger();
            Stream<String> stream = Stream.of("old", "king", "cole", "was", "a", "merry", "old", "soul").parallel();
            stream.filter( e->{
                ai.incrementAndGet();
                return e.contains("o");
            }).allMatch(x->x.indexOf("o")>0);

            System.out.println("AI = "+ai);
        }
    }
Just tried reproducing the same result, while this code runs, the first part is very 'random' (you see 8's, 7's etc etc..) but the longer it runs, you only see 4's, occasionally a few other numbers. When it's nearing it's end of the execution, 99% of the results are a 4.

while typing this post i'm reproducing it again with 1.000.000 cycles instead of 100.000 and it seems like it's actually just a coincidence that I see a lot of 4's. So nevermind!

Re: enthuware.ocpjp.v8.2.1842 (1z0-813 / 02-Concurrency)

Posted: Sun Aug 09, 2020 1:45 pm
by Javier
Hi Paul!
So the JVM is free to "ignore" the order parallel() and run the stream sequentially?
Thank you Paul

Re: enthuware.ocpjp.v8.2.1842 (1z0-813 / 02-Concurrency)

Posted: Sun Aug 09, 2020 11:40 pm
by admin
Not the JVM but the implementation of the stream library. The JVM executes the threads. The stream pipeline is just the Java class library. It may chose to ignore parallel execution and do a sequential execution. You may want to check out the details about from any good book on Java streams.

Re: enthuware.ocpjp.v8.2.1842 (1z0-813 / 02-Concurrency)

Posted: Fri Feb 19, 2021 2:17 pm
by noeloo
How can it be 2 or 3?
I only see two possibilities: either the stream can be split not equally (first stream: "old", "king", "cole", "was", "a", "marry"; second stream: "old", "soul") or the order does not matter during splitting (first stream: "old", "king", "cole", "was"; second stream: "old", "a", "marry", "soul").

Re: enthuware.ocpjp.v8.2.1842 (1z0-813 / 02-Concurrency)

Posted: Sat Feb 20, 2021 1:12 am
by admin
There is no rule that says the stream has to be split equally into two.

Re: enthuware.ocpjp.v8.2.1842 (1z0-813 / 02-Concurrency)

Posted: Sat Feb 20, 2021 8:06 am
by noeloo
Ok, so I guess it can be split like in my first example - quite not equally.
But what about the order? Can the second example also have place?

Re: enthuware.ocpjp.v8.2.1842 (1z0-813 / 02-Concurrency)

Posted: Sat Feb 20, 2021 10:57 am
by admin
The API does not define the exact mechanism by which the library must implement parallel streams. It is possible that as processors and JVMs become more powerful/feature rich, the API may change the implementation to enhance performance. So, in case of parallel streams, pretty much anything is possible. We should never depend on the order or the split.