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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
foxxbl
Posts: 4
Joined: Tue Nov 29, 2016 12:32 pm
Location: Dublin,Ireland
Contact:

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

Post 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

admin
Site Admin
Posts: 8906
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

It could be in both because of the usage of AtomicInteger.
If you like our products and services, please help us by posting your review here.

ramon.carrascom
Posts: 19
Joined: Sun Aug 27, 2017 12:35 pm
Contact:

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

Post 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.

admin
Site Admin
Posts: 8906
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

ramon.carrascom
Posts: 19
Joined: Sun Aug 27, 2017 12:35 pm
Contact:

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

Post 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!! :?

admin
Site Admin
Posts: 8906
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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."
If you like our products and services, please help us by posting your review here.

ramon.carrascom
Posts: 19
Joined: Sun Aug 27, 2017 12:35 pm
Contact:

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

Post 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!!

moostebring
Posts: 3
Joined: Tue Jul 21, 2020 3:28 am
Contact:

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

Post 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.

admin
Site Admin
Posts: 8906
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

Please post the exact code that you ran.
If you like our products and services, please help us by posting your review here.

moostebring
Posts: 3
Joined: Tue Jul 21, 2020 3:28 am
Contact:

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

Post 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!

Javier
Posts: 59
Joined: Mon Feb 20, 2017 12:31 pm
Contact:

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

Post by Javier »

Hi Paul!
So the JVM is free to "ignore" the order parallel() and run the stream sequentially?
Thank you Paul

admin
Site Admin
Posts: 8906
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests