In the development, some asynchronous operations will obviously speed up the execution speed and bring better experience, but at the same time, it also increases the development complexity. If you want to use many threads, you must understand from these aspects
- wait() notify() notifyall() method of thread
- Thread returns Future asynchronously
- ThreadLocal class
- Thread pool ThreadPoolExecutor
- Synchronization tool classes countdownlatch, cyclicbarrier, semaphore, phase, exchange
It is estimated that each of the above is a nightmare for java students of 2-3 years, which is difficult to understand. In this paper, CountDownLatch and CyclicBarrier are briefly mentioned
CountDownLatch
CountDownLatch is commonly used in decomposable tasks with long execution time, and it is the easiest to understand synchronization tool class.
Example 1:
A simple example: there is a one million excel data export, which needs to find out the data from the database, encapsulate it into excel data and output it to the front end.
A little analysis shows that this operation will definitely take a lot of time. Its bottleneck lies in querying database data and writing excel. If I read the database and write excel into a file every 100000 pages of data, and finally use zip to package all excel and multithread, because reading the database will not be locked, the performance will be improved by a magnitude (with practice), At this time, there will be a problem. After I start multiple threads, how can the main thread know that all threads have been completed? Only after all threads have been completed can all Excel files be packaged. At this time, CountDownLatch can be used. The pseudo code is as follows:
// Excel thread class ExcelThread extend Thread{ private CountDownLatch countDownLatch; public ExcelThread(CountDownLatch countDownLatch){ this.countDownLatch = countDownLatch; } public void run(){ try{ // do query db & write excel }finally{ countDownLatch.countDown(); } } } public static void main(){ //Suppose to generate 3 excel CountDownLatch countdownlatch = new CountDownLatch(3); foreach : new ExcelThread(countdownlatch).start(); countDownLatch.await(); // do zip compress & down }
Example two:
If you have used the IDM download tool, you must know that it is multi-threaded download based on its download progress. In fact, it can be multi-threaded download by using the RandomAccessFile of java and countdownload. You can instantiate multiple randomaccessfiles and make them point to different file locations, and then fill in the data. The main thread is divided into Verify the integrity of the file after all threads are completed.
Of course, IDM is better. It will finish all other threads. If a certain section is still stuck, continue to separate and download multiple threads. Of course, the speed will be faster.
Example three:
For CountDownLatch, other threads are game players, such as king glory, and the main thread is the thread controlling the start of the game. Before all players are ready, the main thread is waiting, that is, the game cannot start. When all players are ready, the next step is to start the game with the action executor as the main thread.
CyclicBarrier
For example, when CountDownLatch is waiting in the main thread, CyclicBarrier is waiting for each other among the sub threads, and the main thread has already ended. When the sub threads wait for each other, a thread with similar CountDownLatch function can be attached. After all the sub threads have completed their operations, CyclicBarrier can be reused. So much, it depends on the way it is used I know what it means.
I don't know if the readers have played games like five people and six feet. Each player is a thread, and they have to wait for each other to be ready before they can carry out the next operation. Among them, a commander can also wait for all the students to complete the preparation and issue the instructions, left foot, right foot... The pseudo code is as follows:
// Gamer threads class GamePerson extend Thread{ private CyclicBarrier cyclicBarrier; public GamePerson(CyclicBarrier cyclicBarrier){ this.cyclicBarrier = cyclicBarrier; } public void run(){ // Preparation for the next step prepareNextStep(); cyclicBarrier.await(); // Take the next step nextStep(); } } // Main thread public static void main(){ CyclicBarrier cyclicBarrier = new CyclicBarrier(5); //Build 5 players foreach: new GamePerson(cyclicBarrier); // The game starts and the timing starts startGame();beginCountTime(); foreach:gamepersons gameperson.start(); } // Main thread, add Commander class Leader extend Thread{ public void run(){ System.out.println("Start walking. "); } } // Main thread public static void main(){ CyclicBarrier cyclicBarrier = new CyclicBarrier(5,new Leader()); //Build 5 players foreach: new GamePerson(cyclicBarrier); // The game starts and the timing starts startGame();beginCountTime(); foreach:gamepersons gameperson.start(); } // The previous example can only take one step. If you need to reuse the CyclicBarrier, you need to treat the main thread as a synchronization object. The code is as follows public static void main(){ CyclicBarrier cyclicBarrier = new CyclicBarrier(6); //Build 5 players foreach: new GamePerson(cyclicBarrier); // The game starts and the timing starts startGame();beginCountTime(); //Let's say they take 10 steps for(int i=0;i<10;i++){ cyclicBarrier.reset(); foreach:gamepersons gameperson.start(); cyclicBarrier.await(); } }
A little promotion
It's not easy to create. I hope to support my open source software and my gadgets. Welcome to gitee to click stars, fork, and mention bug s.
Excel general import and export, support excel formula
Blog address: https://blog.csdn.net/sanri1993/article/details/100601578
gitee: https://gitee.com/sanri/sanri-excel-poi
Use template code, generate code from database, and some small tools often used in projects
Blog address: https://blog.csdn.net/sanri1993/article/details/98664034
gitee: https://gitee.com/sanri/sanri-tools-maven