Java DelayQueue deferred queue 21.7.3 ------- thinking java 4
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 extended queue priority queue ** @ author lenovo **/class DelayedTask implements Runnable, Delayed {private static int counter = 0; private final int id = counter ++; // The id ranges from 0 + + private final int delta; // The number of milliseconds to stop the private final long trigger; // nanosecond stop // Save the order in which the task is created protected static List
Sequence = new ArrayList
(); Public 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 () {// One id corresponds to a task in the order of security to execute the task return String. format ("[% 1 $-4d]", delta) + "Task" + id;} public String summary () {return "(" + id + ":" + del Ta + ")";} // sorting trigger @ Overridepublic int compareTo (Delayed o) {DelayedTask that = (DelayedTask) o; if (trigger <that. trigger) return-1; if (trigger> that. trigger) return 1; return 0;}/*** tells you how long the delay expires, or how long the delay has expired */@ Overridepublic long getDelay (TimeUnit unit) {// object expiration time-current time = execution time return unit. convert (trigger-System. nanoTime (), TimeUnit. NANOSECONDS);} // the specific way to close all things is to put it as the last element of the queue. Public static class EndSentinel extends DelayedTask {private ExecutorService exec; public EndSentinel (int delayInMilliseconds, ExecutorService c) {super (milliseconds); 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 () ;}}// it is a task class DelayedTaskConsumer implements Runnable {private DelayQueue
Q; public DelayedTaskConsumer (DelayQueue
Q) {this. q = q ;}@ Overridepublic void run () {try {while (! Thread. interrupted () {q. take (). run (); // run task with the current thread and the task running by the current thread} catch (InterruptedException e) {System. out. println ("exit in an acceptable way ="); // exit in an acceptable way} System. out. println ("finished delayedTaskConsumer") ;}} public class DelayQueueDemo {public static void main (String [] args) {Random rand = new Random (47); ExecutorService exec = Executors. newCachedThreadPool (); DelayQueue
Queue = new DelayQueue
(); // Fill with tasks that have random delays: a task with full random latency for (int I = 0; I <20; I ++) {queue. put (new DelayedTask (rand. nextInt (5000);} // sets the stop point queue. add (new DelayedTask. endSentinel (5000, exec);exec.exe cute (new DelayedTaskConsumer (queue);}/** output: [128] Task 11 run [200] Task 7 run [429] Task 5 run [520] Task 18 run [555] Task 1 run [961] Task 4 run [998] Task 16 run [1207] Task 9 run [1693] Task 2 run [1809] Task 14 run [1861] Task 3 run [2278] Task 15 run [3288] Task 10 run [3551] Task 12 run [4258] Task 0 run [4258] Task 19 run [4522] Task 8 run [4589] Task 13 run [4861] Task 17 run [4868] Task 6 run (0: 8) () (2: 1693) (3: 1861) (4: 961) (6: 4868) (10: 3288) () (13: 4589) (14: 1809) (15: 2278) (16: 998) (17: 4861) [2, 5000] Task 20 calling shutdownNowfinished delayedTaskConsumer */