Home Java javaTutorial How much does Java multi-threaded concurrent programming improve data processing efficiency?

How much does Java multi-threaded concurrent programming improve data processing efficiency?

Apr 28, 2023 pm 11:46 PM
java

    In the work scenario, we encountered such a requirement: to update related information of other models based on the IP address of the host. The requirements are very simple and only involve general database linkage query and update operations. However, during the coding implementation process, it was found that due to the large number of hosts, it takes a long time to loop through the query and update. It takes about 30-40 seconds to call the interface once. min time to complete the operation.

    Therefore, in order to effectively shorten the execution time of interface methods, consider using multi-threaded concurrent programming methods, taking advantage of the parallel execution capabilities of multi-core processors, and asynchronously processing data, which can greatly shorten the execution time and improve effectiveness.

    A reusable thread pool with a fixed number of threads is used here FixedThreadPool, and the concurrent process control tool provided by the CountDownLatch concurrent tool class is used in conjunction to ensure multi-thread concurrency Normal operation during programming:

    • First, obtain the CPU thread of the running machine through the Runtime.getRuntime().availableProcessors() method Number, used to subsequently set the number of threads in the fixed thread pool.

    • Secondly, , determine the characteristics of the task. If it is a computationally intensive task, set the number of threads to CPU thread number 1, if it is IO For intensive tasks, set the number of threads to 2 * Number of CPU threads . Since the method requires frequent interaction with the database, it is an IO-intensive task.

    • After that, the data is grouped and cut. Each thread processes one grouped data. The number of grouped groups is consistent with the number of threads, and a counter is also created. Object CountDownLatch, call the constructor, the initialization parameter value is the number of threads, ensuring that the main thread waits for all child threads to finish running before performing subsequent operations.

    • Then , call the executorService.execute() method, and rewrite the run method to write business logic and data processing Code, remember to decrement the counter by 1 after executing the current thread. Finally, when all sub-threads are completed, close the thread pool.

    After omitting the business logic code in the work scenario, the general processing method example is as follows:

    public ResponseData updateHostDept() {
    		// ...
    		List<Map> hostMapList = mongoTemplate.find(query, Map.class, "host");
            // split the hostMapList for the following multi-threads task
            // return the number of logical CPUs
            int processorsNum = Runtime.getRuntime().availableProcessors();
            // set the threadNum as 2*(the number of logical CPUs) for handling IO Tasks,
            // if Computing Tasks set the threadNum as (the number of logical  CPUs) + 1
            int threadNum = processorsNum * 2;  
            // the number of each group data 
            int eachGroupNum = hostMapList.size() / threadNum; 
            List<List<Map>> groupList = new ArrayList<>();
            for (int i = 0; i < threadNum; i++) {
                int start = i * eachGroupNum;
                if (i == threadNum - 1) {
                    int end = mapList.size();
                    groupList.add(hostMapList.subList(start, end));
                } else {
                    int end = (i+1) * eachGroupNum;
                    groupList.add(hostMapList.subList(start, end));
                }
            }
            // update data by using multi-threads asynchronously
            ExecutorService executorService = Executors.newFixedThreadPool(threadNum/2);
            CountDownLatch countDownLatch = new CountDownLatch(threadNum);
            for (List<Map> group : groupList) {
                executorService.execute(()->{
                    try {
                        for (Map map : group) {
                        	// update the data in mongodb
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                    	// let counter minus one 
                        countDownLatch.countDown();  
                    }
                });
            }
            try {
            	// main thread donnot execute until all child threads finish
                countDownLatch.await();  
            } catch (Exception e) {
                e.printStackTrace();
            }
            // remember to shutdown the threadPool
            executorService.shutdown();  
            return ResponseData.success();
    }
    Copy after login

    Then after using the multi-threaded asynchronous update strategy, The approximate time required to call the interface has dropped from 30-40 min to 8-10 min, greatly improving execution efficiency.

    It should be noted that the newFixedThreadPool used here to create a thread pool has a flaw that its blocking queue defaults to an unbounded queue, and the default value is Integer.MAX_VALUE is very likely to cause OOM problems. Therefore, you can generally use ThreadPoolExecutor to create a thread pool, and you can specify the number of threads in the waiting queue to avoid OOM problems.

    public ResponseData updateHostDept() {
    		// ...
    		List<Map> hostMapList = mongoTemplate.find(query, Map.class, "host");
            // split the hostMapList for the following multi-threads task
            // return the number of logical CPUs
            int processorsNum = Runtime.getRuntime().availableProcessors();
            // set the threadNum as 2*(the number of logical CPUs) for handling IO Tasks,
            // if Computing Tasks set the threadNum as (the number of logical  CPUs) + 1
            int threadNum = processorsNum * 2;  
            // the number of each group data 
            int eachGroupNum = hostMapList.size() / threadNum; 
            List<List<Map>> groupList = new ArrayList<>();
            for (int i = 0; i < threadNum; i++) {
                int start = i * eachGroupNum;
                if (i == threadNum - 1) {
                    int end = mapList.size();
                    groupList.add(hostMapList.subList(start, end));
                } else {
                    int end = (i+1) * eachGroupNum;
                    groupList.add(hostMapList.subList(start, end));
                }
            }
            // update data by using multi-threads asynchronously
            ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 8, 30L, TimeUnit.SECONDS, 
                    new ArrayBlockingQueue<>(100));
            CountDownLatch countDownLatch = new CountDownLatch(threadNum);
            for (List<Map> group : groupList) {
                executor.execute(()->{
                    try {
                        for (Map map : group) {
                        	// update the data in mongodb
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                    	// let counter minus one 
                        countDownLatch.countDown();  
                    }
                });
            }
            try {
            	// main thread donnot execute until all child threads finish
                countDownLatch.await();  
            } catch (Exception e) {
                e.printStackTrace();
            }
            // remember to shutdown the threadPool
            executor.shutdown();  
            return ResponseData.success();
    }
    Copy after login

    In the above code, the number of core threads and the maximum number of threads are 5 and 8 respectively. They are not set to very large values, because if they are set to a large value, frequent interruptions between threads will occur. Context switching will also increase time consumption, but will not maximize the advantages of multi-threading. As for how to choose appropriate parameters, it needs to be determined based on the parameters of the machine and the type of task.

    Finally, if you want to obtain the number of CPU threads of the machine through non-coding methods, it is also very simple. In the Windows system, you can view the number of CPU threads through the Task Manager and select "Performance". , as shown in the picture below:

    How much does Java multi-threaded concurrent programming improve data processing efficiency?

    As you can see from the picture above, the cores in my machine are eight CPUs, but one physical CPU core can be simulated through hyper-threading technology into two logical CPU threads, so my machine supports 8 cores and 16 threads.

    The above is the detailed content of How much does Java multi-threaded concurrent programming improve data processing efficiency?. For more information, please follow other related articles on the PHP Chinese website!

    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    AI Hentai Generator

    AI Hentai Generator

    Generate AI Hentai for free.

    Hot Article

    R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
    2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    Hello Kitty Island Adventure: How To Get Giant Seeds
    1 months ago By 尊渡假赌尊渡假赌尊渡假赌
    Two Point Museum: All Exhibits And Where To Find Them
    1 months ago By 尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    Square Root in Java Square Root in Java Aug 30, 2024 pm 04:26 PM

    Guide to Square Root in Java. Here we discuss how Square Root works in Java with example and its code implementation respectively.

    Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

    Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

    Random Number Generator in Java Random Number Generator in Java Aug 30, 2024 pm 04:27 PM

    Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

    Armstrong Number in Java Armstrong Number in Java Aug 30, 2024 pm 04:26 PM

    Guide to the Armstrong Number in Java. Here we discuss an introduction to Armstrong's number in java along with some of the code.

    Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

    Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

    Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

    Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

    Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

    In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

    Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

    Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

    See all articles