S
S
SankaSanka2021-09-10 09:05:54
Java
SankaSanka, 2021-09-10 09:05:54

How to use ExecutorService?

I'm trying to transfer a set to 24 threads and from there get a map with the result of the work of all these threads at the output. Does not work. What am I doing wrong? Tell me please.

Ideally, you need to do something so that this program can load any processor up to 100%, regardless of the number of cores. (instead of str+str there will be huge calculations)

package used;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.*;

public class ExecutorServiceExample {
  
  public static Map<String, String> adresWhisW = new ConcurrentHashMap();
  CountDownLatch []cdl = new CountDownLatch[24];
  
  public static void main(String args[]) {
    Set<String> ss=new HashSet();
    
    ss.add("вася");
    ss.add("петя");
   new ExecutorServiceExample(ss);
    
    for (Map.Entry<String, String> entry : adresWhisW.entrySet()) {
      System.out.println(entry.getKey() + " " + entry.getValue());
    }
    
  }


  ExecutorServiceExample(Set<String> ss) {

    for (int tr = 1; tr < 24; tr++) {
      cdl[tr] = new CountDownLatch(24);
    }

    ExecutorService executor;
    executor = Executors.newFixedThreadPool(24);

    for (int tr = 1; tr < 24; tr++) {
      executor.execute(new MyThread(ss));
    }


      for (int tr = 1; tr < 24; tr++) {
        try {
          cdl[tr].await();
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }

  
    executor.shutdown();

  }

  public class MyThread implements Runnable {
    private Set<String> strSet;
    private Map<String, String> outputMap;

    public MyThread(Set<String> strSet) {
      this.strSet = strSet;
      this.outputMap = outputMap;
    }

    @Override
    public void run() {
      for (String str : strSet) {
        str=str+str;
        outputMap.put(str, str);
      }
    
    }
  }

}

Exception in thread "pool-1-thread-2" Exception in thread "pool-1-thread-7" java.lang.NullPointerException
at used.ExecutorServiceExample$MyThread.run(ExecutorServiceExample.java:72)
at java.util.concurrent .ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
java.lang.NullPointerException
at used.ExecutorServiceExample$MyThread.run(ExecutorServiceExample.java:72)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149
) )
at java.lang.Thread.run(Thread.java:748)
Exception in thread "pool-1-thread-5" Exception in thread "pool-1-thread-3" java.lang.NullPointerException
at used.ExecutorServiceExample$MyThread.run(ExecutorServiceExample.java:72)
at java.util.concurrent .ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
java.lang.NullPointerException

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question