Java delayqueue deferred queue 21.7.3-------thinking Java 4

Source: Internet
Author: User

Package Org.rui.thread.newc;import Java.util.arraylist;import Java.util.list;import java.util.random;import Java.util.concurrent.delayqueue;import Java.util.concurrent.delayed;import Java.util.concurrent.ExecutorService; Import Java.util.concurrent.executors;import java.util.concurrent.timeunit;/** * A variant of the deferred queue priority queue * * @author Lenovo * * * * Class Delayedtask implements Runnable, delayed{private static int counter = 0;private final int id = counter++;//ID from 0++p Rivate Final int delta;//How many milliseconds to stop the private final long trigger;//nanosecond stop//Save the order that the task was created protected static list<delayedtask> s Equence = new arraylist<delayedtask> ();p ublic delayedtask (int delayinmilliseconds) {delta = delayinmilliseconds; Trigger = System.nanotime () + TimeUnit.NANOSECONDS.convert (delta, Timeunit.milliseconds); Sequence.add (this);} @Overridepublic void Run () {System.out.println (this + "Run");} Public String toString () {///1 ID corresponds to a task to be executed in order to execute the task return String.Format ("[%1$-4d]", Delta) + "task" + ID;} Public String Summary ({return "(" + ID + ":" + Delta + ")";} Sort trigger @overridepublic int compareTo (Delayed o) {delayedtask that = (delayedtask) o;if (Trigger < That.trigger) Return-1 ; if (trigger > That.trigger) return 1;return 0;} /** * Tell how long the delay expires, or how long the delay has expired */@Overridepublic long Getdelay (timeunit unit) {//Object expiry time-now time = Execution Time return unit.conver T (Trigger-system.nanotime (), timeunit.nanoseconds);} The way to close everything. The specific stripping is to place it as the last element of the queue. public static class Endsentinel extends Delayedtask{private executorservice exec;public endsentinel (int Delayinmilliseconds, Executorservice c) {super (delayinmilliseconds); exec = C;} @Overridepublic void Run () {for (Delayedtask pt:sequence) {System.out.print (pt.summary () + "");} System.out.println (); System.out.println (this + "calling Shutdownnow"); Exec.shutdownnow ();}}} itself is a task class Delayedtaskconsumer implements Runnable{private delayqueue<delayedtask> Q;public Delayedtaskconsumer (delayqueue<delayedtask> q) {this.q = q;} @Overridepublic void Run () {Try{while (!Thread.interrupted ()) {Q.take (). Run ();//Run task with the current thread the task that is running on the currently threads}} catch (Interruptedexception e) { SYSTEM.OUT.PRINTLN ("acceptable way to Exit =");//an acceptable way to exit}system.out.println ("finished Delayedtaskconsumer");}} public class Delayqueuedemo{public static void Main (string[] args) {Random rand = new Random (47); Executorservice exec = Executors.newcachedthreadpool ();D elayqueue<delayedtask> queue = new delayqueue< Delayedtask> ();//fill with tasks this has the random delays: task filled with stochastic delay for (int i = 0; i <; i++) {Queue.put (new Delaye Dtask (Rand.nextint (5000));} Set Stop point Queue.add (New Delayedtask.endsentinel (), exec), Exec.execute (new Delayedtaskconsumer (queue));}} /** output:[128] Task one run[200] Task 7 run[429] Task 5 run[520] task [run[555] Task 1 run[961] Task 4 run[998]  Task run[1207] Task 9 run[1693] Task 2 run[1809] task [run[1861] Task 3 run[2278] task [run[3288] task ten run[3551] Task run[4258] Task 0 run[4258] Task run[4522] task 8 run[4589] Task 13run[4861] Task run[4868] Task 6 run (0:4,258) (1:555) (2:1693) (3:1861) (4:961) (5:429) (6:4,868) (7:200) (8:4,522) (9:1207 ) (10:3288) (11:128) (12:3551) (13:4589) (14:1809) (15:2278) (16:998) (17:4861) (18:520) (19:4258) (20:5000) [2] Task 0 calling Shutdownnowfinished delayedtaskconsumer*/


Java delayqueue deferred queue 21.7.3-------thinking Java 4

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.