A preliminary tutorial on multi-thread synchronization-design and use of Simaphore

Source: Internet
Author: User

In the previous article "Preliminary tutorial on multithreading synchronization -- design and use of Metux", we discussed the design and implementation of Mutux in Doug Lea's concurrent package, this article discusses the design and use of Simaphore, which is also common in multithreaded programming. Simaphore also inherits from the Sync interface. Compared with Mutex, Simaphore adds support for Synchronous counting. A Simaphore represents multiple licenses. You can obtain multiple licenses at a time or release multiple licenses at a time. If you reduce the number of Simaphore licenses to 1, it can be used as a Mutex. The following code demonstrates the basic usage of Simaphore:

  1.  ClassPool {
  2. Static FinalMAX_AVAILABLE = 100;// 100 licenses
  3. Private FinalSemaphore available =NewSemaphore (MAX_AVAILABLE );
  4. Public ObjectGetItem ()Throws InterruptedException{// No synch
  5. Available. acquire ();
  6. ReturnGetNextAvailableItem ();
  7. }
  8.  
  9. Public VoidPutItem (ObjectX ){// No synch
  10. If(MarkAsUnused (x ))
  11. Available. release ();
  12. }
  13.  
  14. // Not a particle ly efficient data structure; just for demo
  15.  
  16. Protected Object[] Items =... whatever kinds of items being managed
  17. Protected Boolean[] Used =New Boolean[MAX_AVAILABLE];
  18.  
  19. Protected Synchronized ObjectGetNextAvailableItem (){
  20. For(IntI = 0; I <MAX_AVAILABLE; ++ I ){
  21. If(! Used [I]) {
  22. Used [I] =True;
  23. ReturnItems [I];
  24. }
  25. }
  26. Return Null;// Not reached
  27. }
  28.  
  29. Protected Synchronized BooleanMarkAsUnused (ObjectItem ){
  30. For(IntI = 0; I <MAX_AVAILABLE; ++ I ){
  31. If(Item = items [I]) {
  32. If(Used [I]) {
  33. Used [I] =False;
  34. Return True;
  35. }
  36. Else
  37. Return False;
  38. }
  39. }
  40. Return False;
  41. }
  42.  
  43. }

It can be seen that Simaphore is an enhancement of Mutex, and Simaphore is also compared with Mutex, the following method is added:

1. public Semaphore (long initialPermits)
Constructor to specify the number of licenses.
2. public synchronized long permits ()
The number of returned licenses.
3. public synchronized void release (long n)
Release n licenses at a time.

Correspondingly, in the implementation of Simaphore, a field is also added, the number of storage licenses:
Protected long permits _;

The acquire and release methods have also changed accordingly:

  1. /** Wait until a permit is available, and take one **/
  2. Public VoidAcquire ()Throws InterruptedException{
  3. If(Thread. Interrupted ())Throw New InterruptedException();
  4. Synchronized(This){
  5. Try{
  6. While(Permits _ <= 0) wait ();
  7. -- Permits _;
  8. }
  9. & 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.