"Java.util.concurrent package source reading" 17 Signal volume Semaphore

Source: Internet
Author: User

The friends who have learned the operating system know the semaphore, and in the Java.util.concurrent package there is also a realization about the semaphore: Semaphore.

From the point of view of code implementation, the semaphore is similar to a lock, and can be seen as a limited shared lock, a shared lock that can only be used by a limited number of threads.

Because there is a count, the semaphore constructor has the parameter permits to set the count:

     Public Semaphore (int  permits) {        new  Nonfairsync (permits);    }

Semaphore also supports fair and unfair modes when it comes to thread queuing issues:

     Public Semaphore (intboolean  Fair) {        newnew  Nonfairsync (permits);    }

When it comes to thread queuing, the front says "lock" in the Abstractqueuedsynchronizer, it implements a similar acquisition lock failure, management wait for the function of the thread. So the realization of the semaphore also needs to use this class.

Abstract Static class extends Abstractqueuedsynchronizer // implementation of unfair mode Static Final class extends Sync // implementation of Fair mode Static Final class extends Sync

The sync class uses the state of Abstractqueuedsynchronizer to store the semaphore count:

        Sync (int  permits) {            setState (permits);        }

Because semaphores are similar to shared locks, the abstractqueuedsynchronizer shared type method is used when acquiring resources and freeing resources.

Again back to the front of the unfair and fair mode, this so-called fairness embodied in the acquisition of the lock: Unfair is later, fair is first come first. Look at the two ways to try to get resources:

        //Unfair Mode        Final intNonfairtryacquireshared (intacquires) {            //Direct check is not there are resources that don't look ahead there's no other queued             for (;;) {                intAvailable =getState (); intRemaining = available-acquires; if(Remaining < 0 | |compareandsetstate (available, remaining))returnremaining; }        }        //Fair Mode        protected intTryacquireshared (intacquires) {             for (;;) {                //let's see if there's a line.                if(Hasqueuedpredecessors ())return-1; intAvailable =getState (); intRemaining = available-acquires; if(Remaining < 0 | |compareandsetstate (available, remaining))returnremaining; }        }

For semaphores, the process of acquiring a resource is a process of updating the resource count. The same is true for freeing resources.

        protected Final BooleanTryreleaseshared (intreleases) {             for (;;) {                intCurrent =getState (); intNext = current +releases; if(Next < current)//Overflow                    Throw NewError ("Maximum Permit count exceeded"); if(Compareandsetstate (current, next))return true; }        }

The realization of the semaphore, with the Abstractqueuedsynchronizer and the foundation of the lock, is very well understood.

"Java.util.concurrent package source reading" 17 Signal volume 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.