Java- high concurrency solution study notes

Ordinary single application concurrency:

1. Use the keyword synchronized to achieve.
5) I want to improve the efficiency and not add synchronized to the method, but also want to ensure the accuracy of the data, and finally use synchronized (userId) to lock at the Controller layer (to ensure that only duplicate data is locked. The reason for using in the Controller is because the transaction will be in the Service It was submitted after the call was completed. I tried to synchronize in the Service. There will be 2 duplicate data in the 150 concurrency, because the transaction has not had time to submit)
Test result: Tested 3 times and 150 concurrency all returned in less than one second, the result is 1 Registration is successful, 149 entries returned to the author has been registered

2. Set the maximum level of things

Distributed environment:

1. Database row-level search, advantages: simple and rude; disadvantages: easy to deadlock & poor performance, it is not recommended for non-database professionals.
Using adding row locks to the database, the experiment found that there will still be 2 duplicate data.
Analysis: The
theoretical result should be 1 success and 149 failures.
Adding a row lock to the select statement of the database must act on a certain record, but when the data is first reported, there is no such data in the database, so the row lock is not added at all, resulting in the second data successfully using the select statement asynchronously.
After the first submission is successful, there is this data in the database, and the select statement successfully added a row lock to this record, so there will be no duplicate data behind. Therefore, this method is not available
2. Solve by the unique index of the database table
3. Queue
4. Distributed lock (such as redis lock, Zookeeper)

Guess you like

Origin blog.csdn.net/a159357445566/article/details/108684940