The use of Java concurrency semaphore

Source: Internet
Author: User

The use of Java concurrency semaphore

I. INTRODUCTION

  It was a great feeling to find out today that watching your favorite team play so well and then write a blog. Now is half the time, taking advantage of this time gap, to talk about Java and the contract of another heavyweight class semaphore, this class is literally understood as "semaphore."

So what is the semaphore? I use a more popular way to explain to you, that is, when the class initializes, given a number a, each thread calls the acquire () method, the first to determine whether a is greater than 0, if greater than 0, a minus 1, and then execute the corresponding thread, if not greater than 0, then it will block, Until the other thread calls the release () method, a plus 1, the thread may have an opportunity to execute. Please forgive me for this vulgar way to explain this problem, but not other blog posts used by the professional "license" and other words, I think the professional thing is for professional people to see, a lot of people want to learn a thing he has never dabbled in, then we can not use professional terminology to describe, If he could understand the so-called jargon, would he still have the necessary to learn?

Two. Use of semaphore

  Scene Description: There is a car park only 5 parking spaces, now there are 100 cars to rob this 5 parking spaces, ideally only five cars at the same time can grab the parking space, then the car did not grab the parking space can only wait until the other car to give up the parking space, only the opportunity to use the parking space.

 Public classCarPark { Public Static voidMain (string[] args) {//Blocking QueuesBlockingqueue<string> Parks =NewLinkedblockingqueue<> (5); Parks.offer ("One Parking Space"); Parks.offer ("Two parking Spaces"); Parks.offer ("Three parking Spaces"); Parks.offer ("Parking Space Four"); Parks.offer ("Five Spaces"); Executorservice Executorservice=Executors.newcachedthreadpool (); //as blog post says, the initial value is 5, and the professional argument is 5 licenses .Semaphore Semaphore =NewSemaphore (5);  for(inti = 0; I < 100; i++) {            Final intNo =i; Thread T1=NewThread (() {                Try {                    /*** To obtain a license, first determine whether the number inside the semaphore is greater than 0, if it is greater than 0, * to obtain a license, and then subtract the initial value 5 by 1, the thread will proceed;                     * Licensed (because 5 threads are already licensed, the number inside the semaphore is 0), * threads block until the thread that has already been licensed, calls the release () method, releases the license,                     * That is, the semaphore internal number plus 1, the thread is possible to obtain a license. */Semaphore.acquire (); /*** The corresponding thread will go to the blocking pair, the corresponding vehicle to get to the parking space, if there is no consistent blocking, * until other vehicles return the parking space. */String Park=Parks.take (); System.out.println ("Vehicle" "+ No +" "Get to:" +Park); Thread.Sleep ((Long) Math.random () * 6000); Semaphore.release (); //The thread releases the license, and the popular one is to add 1 to the semaphore internal number .Parks.offer (Park);//return parkingSYSTEM.OUT.PRINTLN ("vehicle" "+ No +" "leave" +Park); } Catch(interruptedexception e) {e.printstacktrace ();            }            });        Executorservice.execute (t1); }    }}

The use of Java concurrency 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.