0
List<String> lines = FileUtils.readLines(dataFile,"UTF-8");
lines.parallelStream().forEachOrdered(line->processRecord(line, dataFileName)); 

here process record will return string value how to store to any collection / file

could help me for this. thanks for advance.

Amit Bera
  • 7,075
  • 1
  • 19
  • 42
  • What does the `processRecord` do? Does it return something (`return value`) or change some file directly? – miiiii Oct 01 '19 at 11:54
  • processRecord method will take the line which we are passing on loop and convert into xml then after we will call other application we will pass this xml to that application. it will return the response back to us. i will capture request and responses for all the lines. – Gopal 8123775498 Oct 03 '19 at 12:36
  • Well, do you want to store both the request `line` and the response `response from processRecord` to some where? If so, you can use map. If not sure about how, then I can write an answer for you. – miiiii Oct 03 '19 at 12:46
  • file i'm processing , if it is order that will be good to match request and responses. – Gopal 8123775498 Oct 03 '19 at 12:53
  • if you just want to match the request and response then ordering is not required anyways.. – miiiii Oct 03 '19 at 12:57

2 Answers2

0

Instead of using forEachOrdered you could use map to execute and store the result for each of the inputs. You can then collect these into a new list to store the results.

Example:

        List<String> result = lines.parallelStream()
            .map(line->line.toUpperCase())
            .collect(Collectors.toCollection(ArrayList::new));
martijn p
  • 598
  • 4
  • 19
  • Thanks, i'm using like below. List outList= lines.parallelStream() .map(line->processRecord(line,dataFileName)).collect(Collectors.toCollection(ArrayList::new)); how many threads will execute parallelly this method ? – Gopal 8123775498 Oct 03 '19 at 12:48
  • @Gopal8123775498 If i remember correctly, basically what it does is it will "Queue" the execution until a core is available. Basically meaning that the amount of threads it can use to process information is less or equal to the amount of cpu cores you have minus one. So for instance, if you have a cpu with 8 cores, it can use 7 of those at the same time assuming that they're all available. For more info: https://stackoverflow.com/a/30802737/5130640 – martijn p Oct 04 '19 at 07:52
0

[Answer is based on comments]

Assuming, you want to process a list of String and also want to store the response of each processing with input and output, then you can follow the below approach.

 List<String> lines = FileUtils.readLines(dataFile,"UTF-8");
 Map<String, String> result = lines.parallelStream()
       .collect(Collectors.toMap(line -> line, line -> processRecord(line, dataFileName)));

Here, the result (a simple Map<Key, Value>) will contain both.. the key as each single line, and value as each single response of THAT line returned by processRecord method.

how many threads will execute parallelly this method (from comments)

The method parallelStream() uses threads form DefaultForkJoinPool. And this pool has default size equal to the number of cores on your system.

miiiii
  • 1,580
  • 1
  • 16
  • 29