Preface
Recently, I am doing a function of original score statistics. Users set relevant parameters through the front desk, and count in real time in the background and return data. Relatively speaking, there are still many statistical functions, so let’s summarize them here.
The best user experience is that every operation can display data in real time. Within 3 seconds, it should be within the user's tolerance range. Therefore, making a product not only requires considering user interaction design, but also back-end optimization is indispensable.
You can simply look at the above 5 statistics. Overall, there are still quite a lot of statistics. The most important thing is to be real-time, real-time, and real-time (say important things three times). Obviously, timing tasks are unrealistic.
Before the transformation
Program logic
Execute tasks sequentially.png
After the transformation
Program logic
Multitasking parallel processing.png
Multitasking parallel processing is suitable for multi-core CPUs. Multi-threaded tasks performed by single-core CPUs may be counterproductive (context switching and thread creation and destruction will consume resources), especially CPU-intensive tasks.
Code implementation
StatsDemo pseudo code:
/** * Multitasking Parallel Statistics* Creator Kebang.com* Creation time April 16, 2018*/public class StatsDemo { final static SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss"); final static String startTime = sdf.format(new Date()); public static void main(String[] args) throws InterruptedException { CountDownLatch latch = new CountDownLatch(5);// Two runners Stats stats1 = new Stats("Task A", 1000, latch); Stats stats2 = new Stats("Task B", 2000, latch); Stats stats3 = new Stats("Task C", 2000, latch); Stats stats4 = new Stats("Task D", 2000, latch); Stats stats5 = new Stats("Task E", 2000, latch); stats1.start();//Task A starts executing stats2.start();//Task B starts executing stats3.start();//Task C starts executing stats4.start();//Task D starts executing stats5.start();//Task E starts executing latch.await();//Waiting for everyone's task to end System.out.println("All statistics tasks are executed completed:" + sdf.format(new Date())); } static class Stats extends Thread { String statsName; int runTime; CountDownLatch latch; public Stats(String statsName, int runTime, CountDownLatch latch) { this.statsName = statsName; this.runTime = runTime; this.latch = latch; } public void run() { try { System.out.println(statsName+ " do stats begin at "+ startTime); //Simulate task execution time Thread.sleep(runTime); System.out.println(statsName + " do stats complete at "+ sdf.format(new Date())); latch.countDown();//Single task ends, the counter is reduced by one} catch (InterruptedException e) { e.printStackTrace(); } } }}Since we want to return statistics synchronously, we use the CountDownLatch class here, which is a new concurrent tool class added in Java 5. It is very simple to use. The detailed usage steps are given with reference to the above pseudo-code.
CountDownLatch is used to synchronize one or more tasks, forcing them to wait for a set of operations performed by other tasks to complete. The typical usage of CountDownLatch is to divide a program into N independent solver tasks and create a CountDownLatch with a value of N. When each task is completed, countDown will be called on this latch, and the task waiting for the problem to be solved will call the await of this latch and block them themselves until the latch count is over.
For specific source code interpretation, you can refer to: Source code analysis CountDownLatch
Project source code: https://gitee.com/52itstyle/spring-data-jpa
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.