1

I know there have been questions which are similar to mine. However, they seem very outdated (assuming JDK 7, etc.) So, I've been programming python for a while and had to learn Java for university. I know that there is a feature in Python, where you can use a pool of Threads/Processes for mapping a list of values to a function.

from multiprocessing.dummy import Pool as ThreadPool 
pool = ThreadPool(4) 
results = pool.map(my_function, my_array)

I have to use the function on a large set of files and I have to use Java (10) and I want to use multiprocessing. My question is: Does Java have such a feature? If so, whats the best practice to use it properly?

valerius21
  • 423
  • 5
  • 14
  • 1
    @rocksportrocker the answer was valid at the time it was made, now 5 years later, there are other possibilities , like stream().parallel()... – GPI Sep 02 '18 at 14:38
  • 1
    You could use paralell streams, perhaps (depending on the task, whether it is interfering etc.) - but be careful: you said you wanted to process a lot of files. Paralellizing disk access is going to make performance worse, not better! – RealSkeptic Sep 02 '18 at 14:39
  • @GPI - that doesn't mean that the question is not a duplicate. It means that if you want to answer it, you should go and add an answer there, rather than have duplicate questions. – RealSkeptic Sep 02 '18 at 14:40

1 Answers1

1

Yes, you can use parallelStream, for example, convert integer list to string list:

List<Integer> list = List.of(1, 2);

List<String> strings =
        list.parallelStream()
                .map(integer -> String.valueOf(integer)).collect(Collectors.toList());
xingbin
  • 27,410
  • 9
  • 53
  • 103