S
S
SankaSanka2021-09-09 19:44:45
Java
SankaSanka, 2021-09-09 19:44:45

How to rewrite a method for a multi-core processor?

The method works fine under two cores. How to multithread it so that it can load as many cores as you want? The number of cores is not known in advance.
in this form, he does not want to load ( Help please. very necessary.

private Map<String, String> makeAdreses() {
    
        Map<String, String> adresWhisW = new ConcurrentHashMap();
        Thread thread = new Thread(new IndexedRandomThread(adresWhisW));
        thread.start();
        for (int i = 0; i < 1000; i++) {
                t=//громоздкие вычисления которые нельзя разделить на части;
            adresWhisW( t, i);
        }
 
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return adresWhisW;
    }
 
 
    public class IndexedRandomThread implements Runnable {
        private Map<String, String> outputMap;
 
        public IndexedRandomThread(Map<String, String> outputMap) {
            this.outputMap = outputMap;
        }
 
        @Override
        public void run() {
            for (int i = 2000; i < 3000; i++) {
                t=//громоздкие вычисления которые нельзя разделить на части
            outputMap.put( t, i);
 
            }
        }
    }

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