Java Advanced Beacon Semaphore

Source: Internet
Author: User
Tags mutex semaphore

 1. Semaphore concept

Semaphore is a synchronization tool provided after Java1.5, semaphore can maintain access to its own number of threads and provides a synchronization mechanism. Using semaphore, you can control the number of threads that access resources concurrently, obtain a license through acquire (), and release () releases a license if there is no wait.

Semaphore implementation of the function is similar to the toilet has 5 pits, if there are 10 people to the toilet, then at the same time only how many people to go to the toilet? At the same time only 5 people can occupy, when any one of the 5 people out of the way, one of the other 5 waiting for another person to occupy. The other 5 people who are waiting can be given a chance at random, or they can be given an opportunity in the order of first served, depending on the parameter options passed in when constructing the semaphore object. The semaphore object of a single semaphore can implement the function of mutex, and can be obtained by one thread "lock", and then by another thread release "lock", which can be applied to some occasions of deadlock recovery. Therefore, the function of the semaphore object of a single semaphore is common with the synchronized realization of mutual exclusion.

2, the function expands 1, the semaphore often unifies the thread pool to use, for example establishes a fixed size 10 thread pool, the maximum number of threads concurrency is 10, when you commit 20 tasks to the thread pool, the thread pool arranges these 10 threads to receive 20 tasks first 10, When 10 of the priorities are completed, they will receive one of the remaining 10 tasks until all tasks have been performed. However, if the semaphore object is introduced at this time, and the value is 5, then only 5 of the 10 threads in this thread pool can execute concurrently, and this is done with limited access. 2, when the semaphore constructor method is passed in the parameter is 1, at this time the number of threads concurrency is 1, that is, thread-safe, this way can also be done on-site mutual exclusion. Java implements mutex thread synchronization there are three ways synchronized, lock, single semaphore

3, the use of semaphore demo is as follows

  1. public class Semahoredemo {
  2. Public static void main(string[] args) {
  3. Executorservice Executorservice = Executors.newcachedthreadpool ();
  4. final Semaphore Semaphore = new Semaphore (1,true);
  5. For (int i=0;i<10;i++) {
  6. Runnable Runnable = new Runnable () {
  7. @Override
  8. Public void Run() {
  9. try {
  10. Semaphore.acquire ();
  11. } catch (Interruptedexception e) {
  12. E.printstacktrace ();
  13. }
  14. System.err.println ("thread" +thread.currentthread (). GetName () +"Enter, already" + (3-semaphore.availablepermits ()) + "concurrency");
  15. try {
  16. Thread.Sleep ((Long) (Math.random () *));
  17. } catch (Interruptedexception e) {
  18. E.printstacktrace ();
  19. }
  20. System.out.println ("thread" +thread.currentthread (). GetName () +"about to leave");
  21. Semaphore.release ();
  22. System.err.println ("thread" +thread.currentthread (). GetName () +"already left" +"current Concurrency:" + (3- Semaphore.availablepermits ()));
  23. }
  24. };
  25. Executorservice.execute (runnable);
  26. }
  27. Executorservice.shutdown ();
  28. }
  29. }

Java Advanced Beacon Semaphore

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.