Nohttp Package--06 nohttp queue, queue priority

Source: Internet
Author: User
Tags comparable

 Public classMain {/** * Program Entry*/     Public voidstart () {//First, FIFO queue//yolandalinkedqueue queue = new Yolandalinkedqueue (3); //Queue.start (); //second, there are no sequential queuesYolandaqueue queue =NewYolandaqueue (1);        Queue.start (); //adding requests to the queue         for(inti =0; I < -; i++) {Request Request=NewRequest ("Request"+i); if(i = =Ten) request.setpriority (PRIORITY.C); if(i = = the) request.setpriority (PRIORITY.D);        Queue.add (Request); }    }     Public Static voidMain (string[] args) {main main=NewMain ();    Main.start (); }} Public classYolandalinkedqueue {PrivateBlockingqueue<request>Blockingqueue; Privatetaskexecutor[] taskexecutors;  PublicYolandalinkedqueue (intpoolsize) {        //Linkedblockingqueue is a FIFO queueBlockingqueue =NewLinkedblockingqueue<>(); Taskexecutors=NewTaskexecutor[poolsize]; }     Public voidAdd (Request request) {Blockingqueue.add (request); }     Public voidstart () { for(inti =0; i < taskexecutors.length; i++) {Taskexecutors[i]=NewTaskexecutor (Blockingqueue);        Taskexecutors[i].start (); }    }     Public voidStop () { for(Taskexecutor taskexecutor:taskexecutors) {taskexecutor.setrunning (false);        Taskexecutor.interrupt (); }    }} Public classYolandaqueue {PrivateBlockingqueue<request>Blockingqueue; Privatetaskexecutor[] taskexecutors; PrivateAtomicinteger Atomicinteger =NewAtomicinteger ();  PublicYolandaqueue (intpoolsize) {        //if the Comparable#compareto (Object) method does not compare back to 0, then it is unorderedBlockingqueue =NewPriorityblockingqueue<request>(); Taskexecutors=NewTaskexecutor[poolsize]; }     Public voidAdd (Request request) {Request.setorder (Atomicinteger.incrementandget ());    Blockingqueue.add (Request); }     Public voidstart () { for(inti =0; i < taskexecutors.length; i++) {Taskexecutors[i]=NewTaskexecutor (Blockingqueue);        Taskexecutors[i].start (); }    }     Public voidStop () { for(Taskexecutor taskexecutor:taskexecutors) {taskexecutor.setrunning (false);        Taskexecutor.interrupt (); }    }} Public classTaskexecutor extends Thread {PrivateBlockingqueue<request>Blockingqueue; PrivateBoolean isrunning =true;  PublicTaskexecutor (blockingqueue<request>blockingqueue) {         This. Blockingqueue =Blockingqueue; }    /** * @param isrunning the isrunning to set*/     Public voidsetrunning (Boolean isrunning) { This. isrunning =isrunning; } @Override Public voidrun () { while(isrunning) {Request Request=NULL; Try {                //The take method is a blocking method that each call gets to the first task in the queue, and if the queue is empty, the method blocks until the queue has a task that returnsRequest =Blockingqueue.take (); } Catch(interruptedexception e) {return; }            Try{Thread.Sleep ( +); } Catch(interruptedexception e) {e.printstacktrace (); } System. out. println (Request.getname ()); }    }} Public classRequest Implements Comparable<request> {    PrivateString name; PrivatePriority mpriority =priority.b; Private intorder; /** * @param name*/     PublicRequest (String name) {super ();  This. Name =name; }    /** * @return the name*/     PublicString GetName () {returnname; }    /** * @param name the name to set*/     Public voidsetName (String name) { This. Name =name; }    /** * @param mpriority the mpriority to set*/     Public voidsetpriority (priority mpriority) { This. mpriority =mpriority; }    /** * @return the mpriority*/     PublicPriority GetPriority () {returnmpriority; }    /** * @return The order*/     Public intGetOrder () {returnorder; }    /** * @param order the order to set*/     Public voidSetorder (intorder) {         This. Order =order; } @Override Public intCompareTo (Request other) {//returns a positive number representing 1 rows after 2, and a negative number representing 1 rows before 2Priority priority = GetPriority ();//get their priorities.Priority otherpriority =other.getpriority (); returnPriority = = Otherpriority? GetOrder ()-Other.getorder (): Otherpriority.ordinal ()-priority.ordinal (); }} Public enumPriority {/** * Lowest priority level*/A,/** * Default priority*/B,/** * Highest priority*/C,/** * normally not used; In special cases, requests are executed immediately after the queue.*/D}

Nohttp Package--06 nohttp queue, queue priority

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.