Preliminary tutorial on multi-thread synchronization-design and use of Metux

Source: Internet
Author: User

Mutex is a Mutex and is widely used in multi-threaded programming. This article takes the Mutex Implementation of the concurrent toolkit of Doug Lea, which is widely used as an example to discuss it. In Doug Lea's concurrent toolkit, Mutex implements the Sync interface, which is a public interface for all locks, Gates, and condition in the concurrent toolkit, the main classes of Sync are Mutex, Semaphore and its sub-classes, Latch, CountDown, and ReentrantLock. This also reflects the idea of abstract programming, so that we can choose to use different implementations of Sync without changing the code or a small amount of code. The following is the definition of the Sync interface:

  1. Public InterfaceSync {
  2. Public VoidAcquire ()Throws InterruptedException;// Obtain the license
  3. Public BooleanAttempt (LongMsecs)Throws InterruptedException;// Try to get a license
  4. Public VoidRelease ();// Release the license
  5. }

Using Sync can replace the Java synchronized keyword and provide more flexible synchronization control. Of course, it doesn't mean that the concurrent toolkit is an independent technology from Java synchronized. In fact, the concurrent toolkit is also built on the basis of synchronized. You can see this from the analysis of Mutex source code below. The synchronized keyword is valid only in the method or code block, while using Sync can span methods or even synchronize objects by passing between objects. This is more powerful than synchronized.
Note that both acquire () and attempt () in Sync will throw InterruptedException. Therefore, when using Sync and its subclass, you must capture InterruptedException when calling these methods. The release () method does not throw InterruptedException, because wait () may be called in the acquire () and attempt () Methods to wait for other threads to release the lock. Release () simplifies implementation and releases the lock directly, whether or not it is actually held. Therefore, you can call release () on a thread that does not have acquire. Because release () does not throw InterruptedException, we can call release () in catch or finally clause to ensure that the obtained lock can be correctly released. For example:

  1. ClassX {
  2. Sync gate;
  3. //...
  4. Public VoidM (){
  5. Try{
  6. Gate. acquire ();// Block until condition holds
  7. Try{
  8. //... Method body
  9. }
  10. Finally{
  11. Gate. release ();
  12. }
  13. }
  14. Catch(InterruptedExceptionEx ){
  15. //... Evasive action
  16. }
  17. }
  18. }



Mutex is a non-reentrant Mutex lock. Mutex is widely used in before/after synchronization environments that require methods to be crossed. The following is the implementation of Mutex in Doug Lea's concurrent toolkit.

  1. Public ClassMutexImplementsSync {
  2. /** The lock status **/
  3. Protected BooleanInuse _ =False;
  4. Public VoidAcquire ()Throws InterruptedException{
  5. If(Thread. Interrupted ())Throw New InterruptedException();// (1)
  6. Synchronized(This){
  7. Try{
  8. While(Inuse _) wait ();
  9. Inuse _ =True;
  10. }
  11. Catch(InterruptedExceptionEx ){// (2)
  12. Notify ();
  13. ThrowEx;
  14. }
  15. }
  16. }
  17. Public Synchronized VoidRelease (){
  18. Inuse _ =False;
  19. Notify ();
  20. }
  21. Public BooleanAttempt (LongMsecs)Throws InterruptedException{
  22. If(Thread. Interrupted ())Throw New InterruptedException();
  23. Synchronized(This

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.