An example of using java code to simulate highly concurrent operations

In Java, the synchronized keyword and lock lock are used to realize the concurrent access control of resources. At the same time, only one thread is allowed to enter the critical area to access resources (except read lock). The main purpose of this sub control is to solve the problem of data inconsistency caused by multiple threads concurrent with the same resource. In another scenario, multiple copies of a resource can be used at the same time. For example, there are multiple printers in the printer room and multiple pits in the toilet. In this case, Java provides another concurrent access control - concurrent access control of multiple copies of resources. Semaphore used today is one of them.

Java can find potential thread safety problems in our system in the fastest way through code simulation with high concurrency. Here, semaphore and countdownlatch are used together with executorservice for simulation. The main introduction is as follows:

1、Semaphore

This class will be provided after JDK 1.5

Semaphore is a count based semaphore. It can set a threshold. Based on this, multiple threads compete to obtain the license signal and return it after completing their own application. When the threshold is exceeded, the thread application license signal will be blocked. Semaphore can be used to build some object pools and resource pools, such as database connection pool. We can also create semaphore with a count of 1 as a mechanism similar to mutual exclusion lock, which is also called binary semaphore, indicating two mutually exclusive states.

2、CountDownLatch

This class will be provided after JDK 1.5,

Countdownlatch enables a thread to wait for other threads to complete their work before executing. For example, the main thread of an application wants to execute after the thread responsible for starting the framework service has started all the framework services.

Countdownlatch is implemented through a counter. The initial value of the counter is the number of threads. Every time a thread completes its task, the value of the counter will decrease by 1. When the counter value reaches 0, it indicates that all threads have completed the task, and then the threads waiting on the lock can resume executing the task.

As shown below:

The above two classes can be used together to achieve the effect of simulating high concurrency. The following examples are in the form of code:

The above method simulates 5000 requests and 200 concurrent operations at the same time. After observing the final results, it is found that the results of each time are different and inconsistent with expectations. The results are as follows:

Finally, the conclusion is that the add method is non thread safe

How to ensure the thread safety of the add method? Modify the add method as follows:

The results are as follows:

Finally, conclusion: the modified add method is thread safe

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>