Click here to Skip to main content
15,891,649 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I search the database many times,even I have cache some result, it still cost took a long time.
Java
List<Map<Long, Node>> aNodeMapList = new ArrayList<>();
Map<String, List<Map<String, Object>>> cacheRingMap=new ConcurrentHashMap<>();
for (Ring startRing : startRings) {
       for (Ring endRing : endRings) {
           Map<String, Object> nodeMapResult = getNodeMapResult(startRing, endRing,cacheRingMap);
           Map<Long, Node> nodeMap = (Map<Long, Node>) nodeMapResult.get("nodeMap");
           if (nodeMap.size() > 0) {
               aNodeMapList.add(nodeMap);
           }
       }
   }

getNodeMapResult is a function to search database according to startRing, endRing,and cache in cacheRingMap,and next time it may not need to search database if I find the result have exist in
cacheRingMap.

What I have tried:

My leader tell me that multithread technology can be used.So I change it to executorCompletionService,but now I have
a question,is this thread safe when I use concurrentHashMap to cache result in executorCompletionService?
Will it run fast after I change?
Java
int totalThreadCount= startRings.size()*endRings.size();
ExecutorService threadPool2 =  Executors.newFixedThreadPool(totalThreadCount>4?4:2);
CompletionService<Map<String, Object>> completionService = new ExecutorCompletionService<Map<String, Object>>(threadPool2);
for (Ring startRing : startRings) {
        for (Ring endRing : endRings) {
            completionService.submit(new Callable<Map<String, Object>>() {
                    @Override
                    public Map<String, Object>  call() throws Exception {
                        return getNodeMapResult(startRing, endRing,cacheRingMap);
                    }
                });
        }
    }

    for(int i=0;i<totalThreadCount;i++){
    Map<String, Object> nodeMapResult=completionService.take().get();
    Map<Long, Node> nodeMap = (Map<Long, Node>) nodeMapResult.get("nodeMap");
    if (nodeMap.size() > 0) {
        aNodeMapList.add(nodeMap);
    }
    }
Posted
Updated 16-Oct-19 12:59pm
v2
Comments
Catey Category 17-Oct-19 4:15am    
Unfortunately, I don't have a solution for you, but perhaps I can still help out. What your manager tells you is potentially true. Multithreading a search over a Map<t,k> can be faster as long as there are no thread bottlenecks. Think of key methods that are synchronized for example. They should(if implemented correctly) be thread-safe, but might not make your search faster.

I've had this same issue in a 2D graphics rendering engine where I had threads rendering to the same screen in their own region of the screen. This can result in faster rendering, but the key plotting method was synchronized, so in the end all the threads were waiting for each other to plot. In the end, the thread management overhead cost more time then just single-threading the rasterizer, which was blazing fast already.

Perhaps my anecdote can help you test your solution for performance.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900