Implementation of "Java Concurrency Programming" 11, bounded cache

Source: Internet
Author: User
Tags cpu usage

1, bounded cache base class
 Packagecn.xf.cp.ch14;/*** Function: Bounded cache implementation base class * Time: 2:20:00 * File: Baseboundedbuffer.java *@authorAdministrator * *@param<V>*/ Public classBaseboundedbuffer<v>{    Private Finalv[] BUF; Private inttail; Private intHead; Private intcount;  PublicBaseboundedbuffer (intcapacity) {        //initializing an array         This. BUF = (v[])NewObject[capacity]; }        //put a data, the final method cannot be overridden    protected synchronized Final voidDoPut (v V) {Buf[tail]=v; if(++tail = =buf.length) {tail= 0; }        //Insert a method, Total + +++count; }        /*** Take out a data *@return     */    protected synchronized Finalv Dotake () {v v=Buf[head]; Buf[head]=NULL; if(++head = =buf.length) {head= 0; }        --count; returnv; }        //determine if the array is full by judging the Count     Public synchronized Final BooleanIsfull () {returnCount = =buf.length; }         Public synchronized Final BooleanIsEmpty () {returnCount = = 0; }}

2, the determination of preconditions to perform the operation
 Packagecn.xf.cp.ch14;/*** Function: Check the insert and get element operation, then perform the operation, the check does not pass the operation * Time: 2:33:41 * File: Grumpyboundedbuffer.java *@authorAdministrator * *@param<V>*/ Public classGrumpyboundedbuffer<v>extendsBaseboundedbuffer<v>{     PublicGrumpyboundedbuffer (intsize) {        Super(size); }         Public synchronized voidPut (v V)throwsException {//if the queue is full, you cannot insert a new element        if( This. Isfull ()) {            Throw NewException ("Queue exceeded"); }         This. DoPut (v); }        //Similarly, the queue is empty and the new element cannot be removed     Public synchronizedV Take ()throwsException {if( This. IsEmpty ()) {            Throw NewException ("No elements in queue"); }                return  This. Dotake (); }}

3, through polling and sleep to achieve a simple blocking
 Packagecn.xf.cp.ch14;/*** Function: Simple blocking through polling and hibernation * Time: 2:55:54 * File: Sleepyboundedbuffer.java *@authorAdministrator * *@param<V>*/ Public classSleepyboundedbuffer<v>extendsBaseboundedbuffer<v>{    //2s    Private Static Final Longsleep_granularity = 2000;  PublicSleepyboundedbuffer (intcapacity) {        Super(capacity); }        //when you put it in the queue     Public voidPut (v V)throwsinterruptedexception { while(true)        {            //there is no loop lock, otherwise this lock can not be released, not sleep lock, sleep lock, in the sleep when others can not operate, never can have elements out            synchronized( This)            {                //if the queue is not full, then put the element                if(! This. Isfull ()) {                     This. DoPut (v); return; }            }            //otherwise hibernate, quit CPU usageThread.Sleep (sleep_granularity); }    }         PublicV Take ()throwsinterruptedexception { while(true)        {            //there is no loop lock, otherwise this lock can not be released, not sleep lock, sleep lock, in the sleep when others can not operate, there will never be a new element come in            synchronized( This)            {                //if the array is empty, then the data can be fetched                if(! This. IsEmpty ()) {                    return  This. Dotake (); }                //if the queue is empty, sleep for a few seconds and try again} thread.sleep (sleep_granularity); }    }    }

4. Conditional queue
 Packagecn.xf.cp.ch14;/*** function: Use condition queue * Time: 3:32:04 * File: Boundedbuffer.java *@authorAdministrator * *@param<V>*/ Public classBoundedbuffer<v>extendsBaseboundedbuffer<v>{     PublicBoundedbuffer (intcapacity) {        Super(capacity); }        /*** Add Data element *@paramv *@throwsinterruptedexception*/     Public synchronized voidPut (v V)throwsinterruptedexception { while( This. Isfull ()) {            //here hangs the program, will release the lock             This. Wait (); }        //if the queue is not full, then the program is awakened and the new lock is acquired         This. DoPut (v); //end of execution, wake up other queues         This. Notifyall (); }         Public synchronizedV Take ()throwsinterruptedexception { while( This. IsEmpty ()) {             This. Wait (); } v v= This. Dotake ();  This. Notifyall (); returnv; }    }

Implementation of "Java Concurrency Programming" 11, bounded cache

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.