Understanding of results produced by parallel Java 8 streams
Not getting the same results every time when I run a parallel stream for reading a file and processing it.
I have data regarding pizzas and want to have a count of different variables using Map and global variables. I should use global variables only. But when I run my code, I am getting different results every time. The input file is not modified at all.
package Assignment;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class GlobalVariables {
static int vegPizzas = 0;
static int N_V_Pizzas = 0;
static int Size_regular = 0;
static int Size_medium = 0;
static int Size_large = 0;
static int Cheese_Burst = 0;
static int Cheese_regular = 0;
static int cheap_cheese = 0;
static Stream<String> reader;
static int rows = 0;
public static void main(String args) {
// TODO Auto-generated method stub
try {
reader = Files.lines(Paths.get("data/SampleData.csv")).parallel();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
reader.map(x -> x.split(","))
.filter(x -> (!(x[0].equals("NULL") || x[1].equals("NULL") || x[2].equals("NULL") || x[3].equals("NULL"))))
.map(x -> updateCounts(x)).collect(Collectors.toList());
printResults();
System.out.println(rows);//to have a count of rows after filtering of NULL values is done
}
private static void printResults() {
// TODO Auto-generated method stub
System.out.println("veg pizzas: " + vegPizzas);
System.out.println("non veg pizzas: " + N_V_Pizzas);
System.out.println("regular: " + Size_regular + " medium: " + Size_medium + " large: " +Size_large);
System.out.println("cheese burst pizzas: " + Cheese_Burst);
System.out.println("regular cheese burst: " + Cheese_regular);
System.out.println("cheaper cheese burst: " + cheap_cheese);
}
private static Object updateCounts(String x) {
// TODO Auto-generated method stub
// static int vegPizzas = 0;
// static int N_V_Pizzas = 0;
// static int Size_regular = 0;
// static int Size_medium = 0;
// static int Size_large = 0;
// static int Cheese_Burst = 0;
// static int Cheese_regular = 0;
// static int cheap_cheese = 0;
rows++;
int flag_regular = 0;
if(x[9].equals("Y")) {
vegPizzas++;
}else if(x[9].equals("N")) {
N_V_Pizzas++;
}
if(x[6].equals("R")) {
Size_regular++;
flag_regular = 1;
}
else if(x[6].equals("M")) {
Size_medium++;
}
else if(x[6].equals("L")) {
Size_large++;
}
if(x[5].equals("Y")) {
Cheese_Burst++;
if(flag_regular == 1) {
Cheese_regular++;
}
if(Integer.parseInt(x[7]) < 500) {
cheap_cheese++;
}
}
return x;
}
}
//True results or expected results (counts of each variety)
veg: 5303
non-veg: 1786
regular: 1779 medium: 2660 large: 2650
cheese-burst: 3499
regular cheese burst: 900
cheaper cheese burst: 598
//Run -1 results
veg pizzas: 5296
non veg pizzas: 1785
regular: 1779 medium: 2660 large: 2649
cheese burst pizzas: 3498
regular cheese burst: 900
cheaper cheese burst: 598
7060
//Run-2 results
veg pizzas: 5294
non veg pizzas: 1786
regular: 1779 medium: 2659 large: 2648
cheese burst pizzas: 3497
regular cheese burst: 900
cheaper cheese burst: 598
7055
//Run-3 results
veg pizzas: 5303
non veg pizzas: 1786
regular: 1779 medium: 2660 large: 2650
cheese burst pizzas: 3499
regular cheese burst: 900
cheaper cheese burst: 598
7086
I did go through this link. I could not relate my problem with the problem posted in that link. I did notice that if I create a sequential stream I am getting the expected results. Any lead on this can be helpful.
java-8 java-stream
add a comment |
Not getting the same results every time when I run a parallel stream for reading a file and processing it.
I have data regarding pizzas and want to have a count of different variables using Map and global variables. I should use global variables only. But when I run my code, I am getting different results every time. The input file is not modified at all.
package Assignment;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class GlobalVariables {
static int vegPizzas = 0;
static int N_V_Pizzas = 0;
static int Size_regular = 0;
static int Size_medium = 0;
static int Size_large = 0;
static int Cheese_Burst = 0;
static int Cheese_regular = 0;
static int cheap_cheese = 0;
static Stream<String> reader;
static int rows = 0;
public static void main(String args) {
// TODO Auto-generated method stub
try {
reader = Files.lines(Paths.get("data/SampleData.csv")).parallel();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
reader.map(x -> x.split(","))
.filter(x -> (!(x[0].equals("NULL") || x[1].equals("NULL") || x[2].equals("NULL") || x[3].equals("NULL"))))
.map(x -> updateCounts(x)).collect(Collectors.toList());
printResults();
System.out.println(rows);//to have a count of rows after filtering of NULL values is done
}
private static void printResults() {
// TODO Auto-generated method stub
System.out.println("veg pizzas: " + vegPizzas);
System.out.println("non veg pizzas: " + N_V_Pizzas);
System.out.println("regular: " + Size_regular + " medium: " + Size_medium + " large: " +Size_large);
System.out.println("cheese burst pizzas: " + Cheese_Burst);
System.out.println("regular cheese burst: " + Cheese_regular);
System.out.println("cheaper cheese burst: " + cheap_cheese);
}
private static Object updateCounts(String x) {
// TODO Auto-generated method stub
// static int vegPizzas = 0;
// static int N_V_Pizzas = 0;
// static int Size_regular = 0;
// static int Size_medium = 0;
// static int Size_large = 0;
// static int Cheese_Burst = 0;
// static int Cheese_regular = 0;
// static int cheap_cheese = 0;
rows++;
int flag_regular = 0;
if(x[9].equals("Y")) {
vegPizzas++;
}else if(x[9].equals("N")) {
N_V_Pizzas++;
}
if(x[6].equals("R")) {
Size_regular++;
flag_regular = 1;
}
else if(x[6].equals("M")) {
Size_medium++;
}
else if(x[6].equals("L")) {
Size_large++;
}
if(x[5].equals("Y")) {
Cheese_Burst++;
if(flag_regular == 1) {
Cheese_regular++;
}
if(Integer.parseInt(x[7]) < 500) {
cheap_cheese++;
}
}
return x;
}
}
//True results or expected results (counts of each variety)
veg: 5303
non-veg: 1786
regular: 1779 medium: 2660 large: 2650
cheese-burst: 3499
regular cheese burst: 900
cheaper cheese burst: 598
//Run -1 results
veg pizzas: 5296
non veg pizzas: 1785
regular: 1779 medium: 2660 large: 2649
cheese burst pizzas: 3498
regular cheese burst: 900
cheaper cheese burst: 598
7060
//Run-2 results
veg pizzas: 5294
non veg pizzas: 1786
regular: 1779 medium: 2659 large: 2648
cheese burst pizzas: 3497
regular cheese burst: 900
cheaper cheese burst: 598
7055
//Run-3 results
veg pizzas: 5303
non veg pizzas: 1786
regular: 1779 medium: 2660 large: 2650
cheese burst pizzas: 3499
regular cheese burst: 900
cheaper cheese burst: 598
7086
I did go through this link. I could not relate my problem with the problem posted in that link. I did notice that if I create a sequential stream I am getting the expected results. Any lead on this can be helpful.
java-8 java-stream
I got the answer. I did not make the method thread safe as described in the earlier link posted at the end of the above question. Now I am getting the same expected results. I am new to this community, so can someone suggest me if I should remove this question or let it be? If i have remove it then to do so? Thank you
– HackChamp
2 days ago
add a comment |
Not getting the same results every time when I run a parallel stream for reading a file and processing it.
I have data regarding pizzas and want to have a count of different variables using Map and global variables. I should use global variables only. But when I run my code, I am getting different results every time. The input file is not modified at all.
package Assignment;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class GlobalVariables {
static int vegPizzas = 0;
static int N_V_Pizzas = 0;
static int Size_regular = 0;
static int Size_medium = 0;
static int Size_large = 0;
static int Cheese_Burst = 0;
static int Cheese_regular = 0;
static int cheap_cheese = 0;
static Stream<String> reader;
static int rows = 0;
public static void main(String args) {
// TODO Auto-generated method stub
try {
reader = Files.lines(Paths.get("data/SampleData.csv")).parallel();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
reader.map(x -> x.split(","))
.filter(x -> (!(x[0].equals("NULL") || x[1].equals("NULL") || x[2].equals("NULL") || x[3].equals("NULL"))))
.map(x -> updateCounts(x)).collect(Collectors.toList());
printResults();
System.out.println(rows);//to have a count of rows after filtering of NULL values is done
}
private static void printResults() {
// TODO Auto-generated method stub
System.out.println("veg pizzas: " + vegPizzas);
System.out.println("non veg pizzas: " + N_V_Pizzas);
System.out.println("regular: " + Size_regular + " medium: " + Size_medium + " large: " +Size_large);
System.out.println("cheese burst pizzas: " + Cheese_Burst);
System.out.println("regular cheese burst: " + Cheese_regular);
System.out.println("cheaper cheese burst: " + cheap_cheese);
}
private static Object updateCounts(String x) {
// TODO Auto-generated method stub
// static int vegPizzas = 0;
// static int N_V_Pizzas = 0;
// static int Size_regular = 0;
// static int Size_medium = 0;
// static int Size_large = 0;
// static int Cheese_Burst = 0;
// static int Cheese_regular = 0;
// static int cheap_cheese = 0;
rows++;
int flag_regular = 0;
if(x[9].equals("Y")) {
vegPizzas++;
}else if(x[9].equals("N")) {
N_V_Pizzas++;
}
if(x[6].equals("R")) {
Size_regular++;
flag_regular = 1;
}
else if(x[6].equals("M")) {
Size_medium++;
}
else if(x[6].equals("L")) {
Size_large++;
}
if(x[5].equals("Y")) {
Cheese_Burst++;
if(flag_regular == 1) {
Cheese_regular++;
}
if(Integer.parseInt(x[7]) < 500) {
cheap_cheese++;
}
}
return x;
}
}
//True results or expected results (counts of each variety)
veg: 5303
non-veg: 1786
regular: 1779 medium: 2660 large: 2650
cheese-burst: 3499
regular cheese burst: 900
cheaper cheese burst: 598
//Run -1 results
veg pizzas: 5296
non veg pizzas: 1785
regular: 1779 medium: 2660 large: 2649
cheese burst pizzas: 3498
regular cheese burst: 900
cheaper cheese burst: 598
7060
//Run-2 results
veg pizzas: 5294
non veg pizzas: 1786
regular: 1779 medium: 2659 large: 2648
cheese burst pizzas: 3497
regular cheese burst: 900
cheaper cheese burst: 598
7055
//Run-3 results
veg pizzas: 5303
non veg pizzas: 1786
regular: 1779 medium: 2660 large: 2650
cheese burst pizzas: 3499
regular cheese burst: 900
cheaper cheese burst: 598
7086
I did go through this link. I could not relate my problem with the problem posted in that link. I did notice that if I create a sequential stream I am getting the expected results. Any lead on this can be helpful.
java-8 java-stream
Not getting the same results every time when I run a parallel stream for reading a file and processing it.
I have data regarding pizzas and want to have a count of different variables using Map and global variables. I should use global variables only. But when I run my code, I am getting different results every time. The input file is not modified at all.
package Assignment;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class GlobalVariables {
static int vegPizzas = 0;
static int N_V_Pizzas = 0;
static int Size_regular = 0;
static int Size_medium = 0;
static int Size_large = 0;
static int Cheese_Burst = 0;
static int Cheese_regular = 0;
static int cheap_cheese = 0;
static Stream<String> reader;
static int rows = 0;
public static void main(String args) {
// TODO Auto-generated method stub
try {
reader = Files.lines(Paths.get("data/SampleData.csv")).parallel();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
reader.map(x -> x.split(","))
.filter(x -> (!(x[0].equals("NULL") || x[1].equals("NULL") || x[2].equals("NULL") || x[3].equals("NULL"))))
.map(x -> updateCounts(x)).collect(Collectors.toList());
printResults();
System.out.println(rows);//to have a count of rows after filtering of NULL values is done
}
private static void printResults() {
// TODO Auto-generated method stub
System.out.println("veg pizzas: " + vegPizzas);
System.out.println("non veg pizzas: " + N_V_Pizzas);
System.out.println("regular: " + Size_regular + " medium: " + Size_medium + " large: " +Size_large);
System.out.println("cheese burst pizzas: " + Cheese_Burst);
System.out.println("regular cheese burst: " + Cheese_regular);
System.out.println("cheaper cheese burst: " + cheap_cheese);
}
private static Object updateCounts(String x) {
// TODO Auto-generated method stub
// static int vegPizzas = 0;
// static int N_V_Pizzas = 0;
// static int Size_regular = 0;
// static int Size_medium = 0;
// static int Size_large = 0;
// static int Cheese_Burst = 0;
// static int Cheese_regular = 0;
// static int cheap_cheese = 0;
rows++;
int flag_regular = 0;
if(x[9].equals("Y")) {
vegPizzas++;
}else if(x[9].equals("N")) {
N_V_Pizzas++;
}
if(x[6].equals("R")) {
Size_regular++;
flag_regular = 1;
}
else if(x[6].equals("M")) {
Size_medium++;
}
else if(x[6].equals("L")) {
Size_large++;
}
if(x[5].equals("Y")) {
Cheese_Burst++;
if(flag_regular == 1) {
Cheese_regular++;
}
if(Integer.parseInt(x[7]) < 500) {
cheap_cheese++;
}
}
return x;
}
}
//True results or expected results (counts of each variety)
veg: 5303
non-veg: 1786
regular: 1779 medium: 2660 large: 2650
cheese-burst: 3499
regular cheese burst: 900
cheaper cheese burst: 598
//Run -1 results
veg pizzas: 5296
non veg pizzas: 1785
regular: 1779 medium: 2660 large: 2649
cheese burst pizzas: 3498
regular cheese burst: 900
cheaper cheese burst: 598
7060
//Run-2 results
veg pizzas: 5294
non veg pizzas: 1786
regular: 1779 medium: 2659 large: 2648
cheese burst pizzas: 3497
regular cheese burst: 900
cheaper cheese burst: 598
7055
//Run-3 results
veg pizzas: 5303
non veg pizzas: 1786
regular: 1779 medium: 2660 large: 2650
cheese burst pizzas: 3499
regular cheese burst: 900
cheaper cheese burst: 598
7086
I did go through this link. I could not relate my problem with the problem posted in that link. I did notice that if I create a sequential stream I am getting the expected results. Any lead on this can be helpful.
java-8 java-stream
java-8 java-stream
edited yesterday
Stefan Zobel
2,44031828
2,44031828
asked 2 days ago
HackChamp
238
238
I got the answer. I did not make the method thread safe as described in the earlier link posted at the end of the above question. Now I am getting the same expected results. I am new to this community, so can someone suggest me if I should remove this question or let it be? If i have remove it then to do so? Thank you
– HackChamp
2 days ago
add a comment |
I got the answer. I did not make the method thread safe as described in the earlier link posted at the end of the above question. Now I am getting the same expected results. I am new to this community, so can someone suggest me if I should remove this question or let it be? If i have remove it then to do so? Thank you
– HackChamp
2 days ago
I got the answer. I did not make the method thread safe as described in the earlier link posted at the end of the above question. Now I am getting the same expected results. I am new to this community, so can someone suggest me if I should remove this question or let it be? If i have remove it then to do so? Thank you
– HackChamp
2 days ago
I got the answer. I did not make the method thread safe as described in the earlier link posted at the end of the above question. Now I am getting the same expected results. I am new to this community, so can someone suggest me if I should remove this question or let it be? If i have remove it then to do so? Thank you
– HackChamp
2 days ago
add a comment |
1 Answer
1
active
oldest
votes
Your updateCounts(String x)
method, called by the map
step of the Stream
pipeline is not thread-safe, and it updates static
variables.
So when it is called by multiple threads concurrently, it is expected to produce different results in each run (i.e. the final values of the static
variables will be different in each run).
The Function
passed to map
shouldn't have side effects, especially not when used in a parallel Stream
.
A better way to make this computation with Stream
s:
Create a
PizzaStatistics
class having all the original static counter variables as instance variables.updateCounts
(which should be renamed) would return a newPizzaStatistics
instance where the relevant counters are set to 1. It won't update any static variables.The Stream pipeline would use the terminal operation
reduce
to produce a singlePizzaStatistics
instance that contains the totals.
Isn't there a way to make only variables rather than methods being synchronized? So that if one variable is being edited by one thread, another thread can work on another variable in the same method. I can think of one methodology where every variable update is done in different methods and those methods are thread safe but no the updateCounts(String[ ] x) method
– HackChamp
2 days ago
2
@HackChamp you could make theupdateCounts
method synchronized, but it would still be bad usage of Streams.
– Eran
2 days ago
I have edited my earlier comment. Is that(mentioned in earlier comment) a better way of doing things?
– HackChamp
2 days ago
2
@HackChamp you can changeupdateCounts
to call a different synchronized static method for each variable it has to update. This wayupdateCounts
itself won't have to be synchronized
– Eran
2 days ago
2
@HackChamp it's still not good practice for using the Stream pipeline this way, sincemap
shouldn't have side effects
– Eran
2 days ago
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53944899%2funderstanding-of-results-produced-by-parallel-java-8-streams%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your updateCounts(String x)
method, called by the map
step of the Stream
pipeline is not thread-safe, and it updates static
variables.
So when it is called by multiple threads concurrently, it is expected to produce different results in each run (i.e. the final values of the static
variables will be different in each run).
The Function
passed to map
shouldn't have side effects, especially not when used in a parallel Stream
.
A better way to make this computation with Stream
s:
Create a
PizzaStatistics
class having all the original static counter variables as instance variables.updateCounts
(which should be renamed) would return a newPizzaStatistics
instance where the relevant counters are set to 1. It won't update any static variables.The Stream pipeline would use the terminal operation
reduce
to produce a singlePizzaStatistics
instance that contains the totals.
Isn't there a way to make only variables rather than methods being synchronized? So that if one variable is being edited by one thread, another thread can work on another variable in the same method. I can think of one methodology where every variable update is done in different methods and those methods are thread safe but no the updateCounts(String[ ] x) method
– HackChamp
2 days ago
2
@HackChamp you could make theupdateCounts
method synchronized, but it would still be bad usage of Streams.
– Eran
2 days ago
I have edited my earlier comment. Is that(mentioned in earlier comment) a better way of doing things?
– HackChamp
2 days ago
2
@HackChamp you can changeupdateCounts
to call a different synchronized static method for each variable it has to update. This wayupdateCounts
itself won't have to be synchronized
– Eran
2 days ago
2
@HackChamp it's still not good practice for using the Stream pipeline this way, sincemap
shouldn't have side effects
– Eran
2 days ago
add a comment |
Your updateCounts(String x)
method, called by the map
step of the Stream
pipeline is not thread-safe, and it updates static
variables.
So when it is called by multiple threads concurrently, it is expected to produce different results in each run (i.e. the final values of the static
variables will be different in each run).
The Function
passed to map
shouldn't have side effects, especially not when used in a parallel Stream
.
A better way to make this computation with Stream
s:
Create a
PizzaStatistics
class having all the original static counter variables as instance variables.updateCounts
(which should be renamed) would return a newPizzaStatistics
instance where the relevant counters are set to 1. It won't update any static variables.The Stream pipeline would use the terminal operation
reduce
to produce a singlePizzaStatistics
instance that contains the totals.
Isn't there a way to make only variables rather than methods being synchronized? So that if one variable is being edited by one thread, another thread can work on another variable in the same method. I can think of one methodology where every variable update is done in different methods and those methods are thread safe but no the updateCounts(String[ ] x) method
– HackChamp
2 days ago
2
@HackChamp you could make theupdateCounts
method synchronized, but it would still be bad usage of Streams.
– Eran
2 days ago
I have edited my earlier comment. Is that(mentioned in earlier comment) a better way of doing things?
– HackChamp
2 days ago
2
@HackChamp you can changeupdateCounts
to call a different synchronized static method for each variable it has to update. This wayupdateCounts
itself won't have to be synchronized
– Eran
2 days ago
2
@HackChamp it's still not good practice for using the Stream pipeline this way, sincemap
shouldn't have side effects
– Eran
2 days ago
add a comment |
Your updateCounts(String x)
method, called by the map
step of the Stream
pipeline is not thread-safe, and it updates static
variables.
So when it is called by multiple threads concurrently, it is expected to produce different results in each run (i.e. the final values of the static
variables will be different in each run).
The Function
passed to map
shouldn't have side effects, especially not when used in a parallel Stream
.
A better way to make this computation with Stream
s:
Create a
PizzaStatistics
class having all the original static counter variables as instance variables.updateCounts
(which should be renamed) would return a newPizzaStatistics
instance where the relevant counters are set to 1. It won't update any static variables.The Stream pipeline would use the terminal operation
reduce
to produce a singlePizzaStatistics
instance that contains the totals.
Your updateCounts(String x)
method, called by the map
step of the Stream
pipeline is not thread-safe, and it updates static
variables.
So when it is called by multiple threads concurrently, it is expected to produce different results in each run (i.e. the final values of the static
variables will be different in each run).
The Function
passed to map
shouldn't have side effects, especially not when used in a parallel Stream
.
A better way to make this computation with Stream
s:
Create a
PizzaStatistics
class having all the original static counter variables as instance variables.updateCounts
(which should be renamed) would return a newPizzaStatistics
instance where the relevant counters are set to 1. It won't update any static variables.The Stream pipeline would use the terminal operation
reduce
to produce a singlePizzaStatistics
instance that contains the totals.
edited 2 days ago
answered 2 days ago
Eran
279k37449535
279k37449535
Isn't there a way to make only variables rather than methods being synchronized? So that if one variable is being edited by one thread, another thread can work on another variable in the same method. I can think of one methodology where every variable update is done in different methods and those methods are thread safe but no the updateCounts(String[ ] x) method
– HackChamp
2 days ago
2
@HackChamp you could make theupdateCounts
method synchronized, but it would still be bad usage of Streams.
– Eran
2 days ago
I have edited my earlier comment. Is that(mentioned in earlier comment) a better way of doing things?
– HackChamp
2 days ago
2
@HackChamp you can changeupdateCounts
to call a different synchronized static method for each variable it has to update. This wayupdateCounts
itself won't have to be synchronized
– Eran
2 days ago
2
@HackChamp it's still not good practice for using the Stream pipeline this way, sincemap
shouldn't have side effects
– Eran
2 days ago
add a comment |
Isn't there a way to make only variables rather than methods being synchronized? So that if one variable is being edited by one thread, another thread can work on another variable in the same method. I can think of one methodology where every variable update is done in different methods and those methods are thread safe but no the updateCounts(String[ ] x) method
– HackChamp
2 days ago
2
@HackChamp you could make theupdateCounts
method synchronized, but it would still be bad usage of Streams.
– Eran
2 days ago
I have edited my earlier comment. Is that(mentioned in earlier comment) a better way of doing things?
– HackChamp
2 days ago
2
@HackChamp you can changeupdateCounts
to call a different synchronized static method for each variable it has to update. This wayupdateCounts
itself won't have to be synchronized
– Eran
2 days ago
2
@HackChamp it's still not good practice for using the Stream pipeline this way, sincemap
shouldn't have side effects
– Eran
2 days ago
Isn't there a way to make only variables rather than methods being synchronized? So that if one variable is being edited by one thread, another thread can work on another variable in the same method. I can think of one methodology where every variable update is done in different methods and those methods are thread safe but no the updateCounts(String[ ] x) method
– HackChamp
2 days ago
Isn't there a way to make only variables rather than methods being synchronized? So that if one variable is being edited by one thread, another thread can work on another variable in the same method. I can think of one methodology where every variable update is done in different methods and those methods are thread safe but no the updateCounts(String[ ] x) method
– HackChamp
2 days ago
2
2
@HackChamp you could make the
updateCounts
method synchronized, but it would still be bad usage of Streams.– Eran
2 days ago
@HackChamp you could make the
updateCounts
method synchronized, but it would still be bad usage of Streams.– Eran
2 days ago
I have edited my earlier comment. Is that(mentioned in earlier comment) a better way of doing things?
– HackChamp
2 days ago
I have edited my earlier comment. Is that(mentioned in earlier comment) a better way of doing things?
– HackChamp
2 days ago
2
2
@HackChamp you can change
updateCounts
to call a different synchronized static method for each variable it has to update. This way updateCounts
itself won't have to be synchronized– Eran
2 days ago
@HackChamp you can change
updateCounts
to call a different synchronized static method for each variable it has to update. This way updateCounts
itself won't have to be synchronized– Eran
2 days ago
2
2
@HackChamp it's still not good practice for using the Stream pipeline this way, since
map
shouldn't have side effects– Eran
2 days ago
@HackChamp it's still not good practice for using the Stream pipeline this way, since
map
shouldn't have side effects– Eran
2 days ago
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53944899%2funderstanding-of-results-produced-by-parallel-java-8-streams%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I got the answer. I did not make the method thread safe as described in the earlier link posted at the end of the above question. Now I am getting the same expected results. I am new to this community, so can someone suggest me if I should remove this question or let it be? If i have remove it then to do so? Thank you
– HackChamp
2 days ago