-1

I am traying to get a small idea about Threads. I want to : for instance

-ExecutorService: check for how many threads it will find the max of the array faster
when using 2/4/6/8 then to sout :

   best time
   average time
   worst time

What I did is :

Main class:

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadEx {
    public static void main(String[] args) {

        ExecutorService executorService = Executors.newFixedThreadPool(2); // example using 2 threads
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); // I have to make max
        Runnable run = () -> {
            list.parallelStream()
                    .map(sum -> list.stream()   // I don't know how to make it here do  only 
print once the sum not to print the sum  every time  for each element  = 10 times = 10 elements
                                .mapToInt(Integer::intValue)
                                .max())
                        .forEach(System.out::println);

        };


        long start = System.currentTimeMillis(); //Start time

        executorService.execute(run); // running the task

        long end = System.currentTimeMillis(); //End time
        System.out.println(end - start); // time spent total


        executorService.shutdown();
    }
}

So short story I want to make sum of elements using 2 threads(as in my example)and then sout the time spent for executing the task Further I want to make same method but using Executors.newFixedThreadPool(4)(6)(8) threads and so on

I assume the fact that I use a Runnable to be able to make a sum is not a good idea.

since if I call

   Integer run = list.stream()
            .mapToInt(Integer::intValue)
            .sum();

It cannot be because it must return an int

Can someone give me and hint please? Any advice/hints/minuses are welcome Thanks

BecaNr1
  • 33
  • 5
  • 2
    Several issues with this code. Why are you retrieving a sum if you want the max of the array? Why do you not just accept a `long` as the result? Also, for how to write a performance benchmark, [read this](https://stackoverflow.com/questions/504103/how-do-i-write-a-correct-micro-benchmark-in-java) – daniu May 10 '22 at 08:22
  • Regarding the max of the array you are right , I must find max , I realize now that I do it wrong ;( , I can work with `Long` that is should be fine also . Thanks for advice .:) I will edit my post – BecaNr1 May 10 '22 at 08:27
  • 1
    Also, you won't find a difference between how large your thread pool is; only one thread in your pool will get used. The parallelStream() will be the point your code uses multithreading, and it will use the common forkjoinpool. – daniu May 10 '22 at 08:36
  • So I can use `List list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); Runnable run = () -> System.out.println(list.stream().max(Comparator.comparing(Integer::intValue)));` To get my max element like this ? – BecaNr1 May 10 '22 at 08:50
  • Actually is not correct what I said in the comment above because I return Optional and is not ok : this is the correct way : `Runnable run = () -> System.out.println("Max value is : " + Collections.max(list));` – BecaNr1 May 10 '22 at 10:38

0 Answers0