Synchronization code example of Java Concurrent Programming
Synchronizers are objects that enable a thread to wait for another thread, allowing them to coordinate actions. The most commonly used synchronizers are countdownlatch and semaphore, and the less commonly used ones are barrier and exchange
The queue synchronizer abstractqueuedsynchronizer is the basic framework used to build locks or other synchronization components. It internally uses a volatile modified int type member variable state to represent the synchronization status, and completes the queuing of resource acquisition threads through the built-in FIFO queue.
The main use method of synchronizer is inheritance. Subclasses manage the synchronization state by inheriting the synchronizer and implementing its abstract methods. It is inevitable to modify the synchronization state during the implementation of the abstract methods. At this time, three methods provided by the synchronizer (getstate(), setstate (intnewstate) / and compareandsetstate (intexpect, intupdate)) need to be used for operation, Because they can ensure that the change of state is safe. The subclass is recommended to be defined as the static internal class of the user-defined synchronization component. The synchronizer itself does not implement any synchronization interface. It only defines several release methods for obtaining synchronization status for the user-defined synchronization component. The synchronizer can obtain synchronization status exclusively or shared, This makes it easy to implement different types of synchronization components (reentrantlock, readwritelock, countdownlatch, etc.).
Synchronizer is the key to realize lock. In the realization of lock, synchronizer is aggregated and used to realize the semantics of lock. The direct relationship between them is: lock is user-oriented, which defines the interface between user and lock and hides the details of implementation; The synchronizer is a lock oriented implementer, which simplifies the implementation of the lock and shields the underlying operations such as synchronization state management, thread queuing, waiting and wake-up. Lock and synchronizer well isolate the areas that users and implementers need to pay attention to.
The design of the synchronizer is based on the template method pattern. Users need to inherit the synchronizer and rewrite this method, then combine the synchronizer in the implementation of custom synchronization components and call the template methods provided by the synchronizer, and these template methods will call the methods rewritten by the user.
The template methods provided by the synchronizer are basically divided into three categories: exclusive acquisition and release of lock synchronization status, shared acquisition and release of synchronization status, and query of waiting threads in the synchronization queue. The custom synchronization component will use the template method provided by the synchronizer to implement its own synchronization semantics. The countdown latch is a one-time barrier that allows one or more threads to wait for one or more other threads to do something. The only constructor of countdownlatch takes an int parameter, which refers to the number of times that countdown method must be called on the latch before waiting for the thread to be processed.
EG:
summary
The above is all about the synchronizer code example of Java Concurrent Programming in this paper. I hope it will be helpful to you. Interested friends can continue to refer to this website:
This paper deeply analyzes the implementation principle of volatile in Java Concurrent Programming
Java Web applications use stream limiting to handle a large number of concurrent requests
Implementation of BlockingQueue for Java Concurrent learning