Preliminary tutorial on multithreading synchronization-design and use of reentrant locks

Source: Internet
Author: User

In the previous article, Mutex is an exclusive lock. Only one thread can own the lock, and it is the same thread immediately. If a Mutex is already held, it will still block the attempt to obtain the lock again. Sometimes we need to lock the same thread as the Java synchronized to re-enter, as long as it already has the lock, instead of blocking it. We can transform the implementation of Mutex in the previous article to implement a reentrant lock-ReentrantLock. This requires ReentrantLock to record the owner (thread) of the current lock, and set an integer variable to record the number of times the current thread enters.

  1. Public ClassReentrantLockImplementsSync {
  2. Protected ThreadOwner _ =Null;
  3. Protected LongHolds _ = 0;
  4. //......
  5. }

When obtaining and releasing a lock, first determine whether the thread is the lock owner. If the current thread already owns the lock, increase 1 at each acquire (), and decrease 1 at release () to 0, this indicates that the current owner of the lock has completely released the lock and no longer owns the lock. Therefore, set the owner to null. If the current thread is not the lock owner, wait () will be locked in an attempt to obtain the lock. In the release () method, if the owner has completely released the lock, the owner will be cleared, and y () Other threads.

  1. Public VoidAcquire ()Throws InterruptedException{
  2. If(Thread. Interrupted ())Throw New InterruptedException();
  3. ThreadCaller =Thread. CurrentThread ();
  4. Synchronized(This){// Synchronize on this
  5. If(Caller = owner _)
  6. ++ Holds _;
  7. Else{
  8. Try{
  9. While(Owner _! =Null) Wait ();
  10. Owner _ = caller;
  11. Holds _ = 1;
  12. }
  13. Catch(InterruptedExceptionEx ){
  14. Notify ();
  15. ThrowEx;
  16. }
  17. }
  18. }
  19. }
  20. Public Synchronized VoidRelease (){// Synchronize on this
  21. If(Thread. CurrentThread ()! = Owner _)
  22. Throw New Error("Illegal Lock usage ");
  23. If(-- Holds _ = 0 ){
  24. Owner _ =Null;
  25. Notify ();
  26. }
  27. }


Note that the above code needs to synchronize owner _ and holds _ on this to solve the Race Condition on these two variables. The attempt () method is similar to Mutex, and the check and count of the lock owner are also added:

  1. Public BooleanAttempt (LongMsecs)Throws InterruptedException{
  2. If(Thread. Interrupted ())Throw New InterruptedException();
  3. ThreadCaller =Thread. CurrentThread ();
  4. Synchronized(This){
  5. If(Caller = owner _){
  6. ++ Holds _;
  7. Return True;
  8. }
  9. Else If(Owner _ =Null){
  10. Owner _ = caller;
  11. Holds _ = 1;
  12. Return True;
  13. }
  14. Else If(Msecs <= & nb

Related Article

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.