enthuware.ocpjp.v8.2.1842 (1z0-813 / 02-Concurrency)
Moderator: admin
-
- Posts: 4
- Joined: Tue Nov 29, 2016 12:32 pm
- Location: Dublin,Ireland
- Contact:
enthuware.ocpjp.v8.2.1842 (1z0-813 / 02-Concurrency)
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
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
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: enthuware.ocpjp.v8.2.1842 (1z0-813 / 02-Concurrency)
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.
-
- Posts: 19
- Joined: Sun Aug 27, 2017 12:35 pm
- Contact:
Re: enthuware.ocpjp.v8.2.1842 (1z0-813 / 02-Concurrency)
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.
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.
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: enthuware.ocpjp.v8.2.1842 (1z0-813 / 02-Concurrency)
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.
Please post the exact code that you are running.
If you like our products and services, please help us by posting your review here.
-
- Posts: 19
- Joined: Sun Aug 27, 2017 12:35 pm
- Contact:
Re: enthuware.ocpjp.v8.2.1842 (1z0-813 / 02-Concurrency)
Of course! Here it is:
And the output:
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);
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!!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
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: enthuware.ocpjp.v8.2.1842 (1z0-813 / 02-Concurrency)
Oh ok, I understand your question now. If you look at the description of allMatch, it clearly says:
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."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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 19
- Joined: Sun Aug 27, 2017 12:35 pm
- Contact:
Re: enthuware.ocpjp.v8.2.1842 (1z0-813 / 02-Concurrency)
Now I see!! I was confused thinking that the culprit was the concurrency, when it really was the allMatch. Thank you so much!!
-
- Posts: 3
- Joined: Tue Jul 21, 2020 3:28 am
- Contact:
Re: enthuware.ocpjp.v8.2.1842 (1z0-813 / 02-Concurrency)
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.
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.
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: enthuware.ocpjp.v8.2.1842 (1z0-813 / 02-Concurrency)
Please post the exact code that you ran.
If you like our products and services, please help us by posting your review here.
-
- Posts: 3
- Joined: Tue Jul 21, 2020 3:28 am
- Contact:
Re: enthuware.ocpjp.v8.2.1842 (1z0-813 / 02-Concurrency)
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);
}
}
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!
-
- Posts: 66
- Joined: Mon Feb 20, 2017 12:31 pm
- Contact:
Re: enthuware.ocpjp.v8.2.1842 (1z0-813 / 02-Concurrency)
Hi Paul!
So the JVM is free to "ignore" the order parallel() and run the stream sequentially?
Thank you Paul
So the JVM is free to "ignore" the order parallel() and run the stream sequentially?
Thank you Paul
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: enthuware.ocpjp.v8.2.1842 (1z0-813 / 02-Concurrency)
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.
-
- Posts: 61
- Joined: Sat Feb 15, 2020 8:56 am
- Contact:
Re: enthuware.ocpjp.v8.2.1842 (1z0-813 / 02-Concurrency)
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").
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").
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: enthuware.ocpjp.v8.2.1842 (1z0-813 / 02-Concurrency)
There is no rule that says the stream has to be split equally into two.
If you like our products and services, please help us by posting your review here.
-
- Posts: 61
- Joined: Sat Feb 15, 2020 8:56 am
- Contact:
Re: enthuware.ocpjp.v8.2.1842 (1z0-813 / 02-Concurrency)
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?
But what about the order? Can the second example also have place?
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: enthuware.ocpjp.v8.2.1842 (1z0-813 / 02-Concurrency)
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.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 19 guests