Use semaphores semaphore to implement mutex lock

Source: Internet
Author: User
In Doug Lea's famous "Java concurrent programming-design principles and patterns", the English name" Concurrent Programming in Java: design principles and patterns, Second Edition", The book mentions that mutex can be implemented using semaphores semaphore. Although Java provides the lock through the synchronize keyword, and uses this infrastructure to implement the semaphore. In some systems, only semaphores are the primitive, and the lock is implemented by semaphores. The Code is as follows:

Import java. util. Concurrent. semaphore;

Public class mutex ...{
Private semaphore S = new semaphore (1 );

Public void acquire () throws interruptedexception ...{
S. Acquire ();
}
Public void release ()...{
S. Release ();
}
Public Boolean attempt (int ms) throws interruptedexception ...{
Return S. tryacquire (MS );
}
}

The above code can only be compiled in Java 5, because semaphore is provided in Java 5. I have questions when reading the above Code. If the release is called twice consecutively and then both threads call acquire, can both threads run simultaneously, thus violating the definition of the mutex lock? To prove my guess, I wrote the following code:

Public class testmutex ...{
Public static void main (string [] ARGs) throws interruptedexception ...{
Mutex = new mutex ();
Mutex. Acquire ();
Mutex. Release ();
Mutex. Release ();
New mythread (mutex). Start ();
New mythread (mutex). Start ();
}

}

Class mythread extends thread ...{
Private mutex;

Public mythread (mutex )...{
This. mutex = mutex;
}

Public void run ()...{
Try ...{
Mutex. Acquire ();
} Catch (interruptedexception E1 )...{
Throw new runtimeexception (E1 );
}
For (INT I = 0; I <10; I ++ )...{
System. Out. Print (I );
If (I % 3 = 0 )...{
Try ...{
Thread. Sleep (100 );
} Catch (interruptedexception e )...{
E. printstacktrace ();
}
}
}
Mutex. Release ();
}
}

The output of this program is as follows:
00123123456456789789
This confirms my guesses.

For comparison, the following is a mutex lock scheme using the synchronized keyword:

Public class testlock ...{
Public static void main (string [] ARGs) throws interruptedexception ...{
New mythread2 (). Start ();
New mythread2 (). Start ();
}

}

Class mythread2 extends thread ...{
Public void run ()...{
Synchronized (testlock. Class )...{
For (INT I = 0; I <10; I ++ )...{
System. Out. Print (I );
If (I % 3 = 0 )...{
Try ...{
Thread. Sleep (100 );
} Catch (interruptedexception e )...{
E. printstacktrace ();
}
}
}
}
}
}

The output of this program is as follows:
01234567890123456789
The two threads are mutually exclusive.

This problem occurs even though "Private semaphore S = new semaphore (1)" in the mutex definition, that is, the initial permits of the semaphore is 1, however, each time the release method is called, the permits plus one is added. If the maximum permits value is 1 and the minimum value is 0, it is the real mutex.

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.