Java PriorityBlockingQueue is a basic priority queue that can block read operations.
Java PriorityBlockingQueue is a basic priority queue that can block read operations.
Package org. rui. thread. newc; import java. util. arrayList; import java. util. list; import java. util. queue; import java. util. random; import java. util. concurrent. executorService; import java. util. concurrent. executors; import java. util. concurrent. priorityBlockingQueue; import java. util. concurrent. timeUnit;/*** this is a preferred-level Queue Based on which read operations can be blocked. * The following is an example. The objects in the queue are tasks that appear in the queue in the order of priority. * PrioritizedTask is assigned a priority number to provide this order. * @ Author lenovo **/class PrioritizedTask implements Runnable, Comparable
{Private Random rand = new Random (47); private static int counter = 0; private final int id = counter ++; private final int priority; protected static List
Sequence = new ArrayList
(); Public PrioritizedTask (int priority) {this. priority = priority; sequence. add (this) ;}@ Overridepublic int compareTo (PrioritizedTask o) {return priority <o. priority? 1: (priority> o. priority? -1: 0) ;}@ Overridepublic void run () {try {TimeUnit. MILLISECONDS. sleep (rand. nextInt (250);} catch (InterruptedException e) {// Acceptable way to exit in an Acceptable way} System. out. println ("PrioritizedTask run:" + this) ;}@ Overridepublic String toString () {return String. format ("[% 1 $-3d]", priority) + "= Task> id:" + id;} public String summary () {return "(id: "+ id +" priority: "+ priority +") ";} // public static class endSentinel extends PrioritizedTask {private ExecutorService exec; public endSentinel (ExecutorService e) {super (-1); // lowest priority in this program lowest priority exec = e;} @ Overridepublic void run () {int count = 0; for (PrioritizedTask pt: sequence) {System. out. println ("summary:" + pt. summary (); if (++ count % 5 = 0) {System. out. println () ;}} System. out. println (); System. out. println (this + "calling shutdownNow"); exec. shutdownNow ();}}} ///////////// producer ///// // class prioritizedTaskProducer implements Runnable {private Random rand = new Random (47 ); private Queue
Queue; private ExecutorService exec; public prioritizedTaskProducer (Queue
Q, ExecutorService e) {queue = q; exec = e; // used for end sentinel used to end sentinel} @ Overridepublic void run () {// unbounded queue; never blocks unbounded queue; never congested // fill it up fast with random priorities fill it in a fast random priority for (int I = 0; I <20; I ++) {int temp = 0; System. out. println ("Incoming queue> random 20> 10> inner:" + (temp = rand. nextInt (10); queue. add (new PrioritizedTask (temp); Thread. yield (); // returns the current thread to the executable state, so that threads with the same priority can enter the execution state, but not absolute. Because the virtual machine may re-execute the thread .} // Trickle in highest-priority jobs work in the highest priority. try {// put 10 10for (int I = 0; I <10; I ++) {System. out. println ("Incoming queue> specify 10> 10:" + 10); TimeUnit. MILLISECONDS. sleep (2500); queue. add (new PrioritizedTask (10) ;}for (int I = 0; I <10; I ++) {System. out. println ("Incoming queue> auto-increment> 10> 0-10:" + I); // add jobs lowest priority first: add the lowest priority: queue. add (new PrioritizedTask (I);} // a sentinel to stop all the tasks to stop all the task queues. add (new PrioritizedTask. endSentinel (exec);} catch (InterruptedException e) {// exit System in an acceptable way. out. println ("InterruptedException> 143");} System. out. println ("completed prioritizedTaskProducer"); }}/// consumer // PriorityBlockingQueue // class prioritizedTaskConsumer implements Runnable {private PriorityBlockingQueue
Q; public prioritizedTaskConsumer (PriorityBlockingQueue
Q) {this. q = q ;}@ Overridepublic void run () {try {while (! Thread. interrupted () {// use current thread to run the task q. take (). run () ;}} catch (InterruptedException e) {// acceptable way to exit} System. out. println ("completed prioritizedTaskConsumer");}/***** main * @ author lenovo **/public class PriorityBlockingQueueDemo {public static void main (String [] args) throws InterruptedException {Random randx = new Random (47); int c = randx. nextInt (250); System. out. println (c); ExecutorService exec = Executors. newCachedThreadPool (); PriorityBlockingQueue
Queue = new PriorityBlockingQueue
();Exec.exe cute (new prioritizedTaskProducer (queue, exec); // producer // thread.sleep(80001_exec.exe cute (new prioritizedTaskConsumer (queue); // consumer}/** output: lupus --- in queue> random 20> 10> inner: 8 in queue> random 20> 10> inner: 5 in queue> random 20> 10> inner: 3 In the queue> random 20> 10> inner: 1 in the queue> random 20> 10> inner: 1 in the queue> random 20> 10> inner: 9 into the queue> random 20> 10> inner: 8 into the queue> random 20> 10> inner: 0 into the queue> random 20> 10> inner: 2 In queue> random 20> 10> inner: 7 in queue> random 20> 10> inner: 8 in queue> random 20> 10> inner: 8 in the queue> random 20> 10> inner: 1 in the queue> random 20> 10> inner: 9 into the queue> random 20> 10> inner: 9 into the queue> random 20> 10> inner: 8 into the queue> random 20> 10> inner: 8 in the queue> random 20> 10> inner: 1 in the queue> random 20> 10> inner: 0 in the queue> random 20> 10> inner: 8 incoming queues> 10> 10: 10> 10> 10> 10> 10> 10: 10> 10> 10: 10 In the queue> specify 10> 10: 10 PrioritizedTask run: [10] = Task> id: 20 PrioritizedTask run: [10] = Task> id: 21 PrioritizedTask run: [10] = Task> id: 22 PrioritizedTask run: [9] = Task> Id: 5 PrioritizedTask run: [9] = Task> id: 13 PrioritizedTask run: [9] = Task> id: 14 PrioritizedTask run: [8] = Task> id: 15 PrioritizedTask run: [8] = Task> id: 16 PrioritizedTask run: [8] = Task> id: 19 PrioritizedTask run: [8] = Task> id: 0 PrioritizedTask run: [8] = Task> id: 10 PrioritizedTask run: [8] = Task> id: 11 PrioritizedTask run: [8] = Task> id: 6 PrioritizedTask run: [7] = Task> Id: 9 PrioritizedTask run: [5] = Task> id: 1 PrioritizedTask run: [3] = Task> id: 2 PrioritizedTask run: [2] = Task> id: 8 PrioritizedTask run: [1] = Task> id: 12 PrioritizedTask run: [1] = Task> id: 17 PrioritizedTask run: [1] = Task> id: 4 PrioritizedTask run: [1] = Task> id: 3 PrioritizedTask run: [0] = Task> id: 7 PrioritizedTask run: [0] = Task> id: 18 into the queue> specify 10> 10: 10 PrioritizedTask run: [10] = Task> id: 23 into the queue> specify 10> 10: 10 PrioritizedTask run: [10] = Task> id: 24 in the queue> specify 10> 10: 10 PrioritizedTask run: [10] = Task> id: 25 in the queue> specify 10> 10: 10 PrioritizedTask run: [10] = Task> id: 26 in the queue> specify 10> 10: 10 PrioritizedTask run: [10] = Task> id: 27 in the queue> specify 10> 10: 10 PrioritizedTask run: [10] = Task> id: 28 incoming queue> auto-increment> 10> 0-incoming queue> auto-increment> 10> 0-incoming queue> auto-increment> 10> 0-incoming queue> auto-increment> 10> 0-> auto-increment> 10> 0- >>> Auto-increment> 10> 0-> auto-increment> 10> 0-> auto-increment> 10> 0->> auto-increment> 10> 0-incoming queue> auto-increment> 10> 0-complete prioritizedTaskProducerPrioritizedTask run: [10] = Task> id: 29 PrioritizedTask run: [9] = Task> id: 39 PrioritizedTask run: [8] = Task> id: 38 PrioritizedTask run: [7] = Task> id: 37 PrioritizedTask run: [6] = Task> id: 36 PrioritizedTask run: [5] = Task> id: 35 PrioritizedTask run: [4] = Task> id: 34 Priori TizedTask run: [3] = Task> id: 33 PrioritizedTask run: [2] = Task> id: 32 PrioritizedTask run: [1] = Task> id: 31 PrioritizedTask run: [0] = Task> id: 30summary summary: (id: 0 priority: 8) summary: (id: 1 priority: 5) summary: (id: 2 priority: 3) summary: (id: 3 priority: 1) summary: (id: 4 priority: 1) summary: (id: 5 priority: 9) summary: (id: 6 priority: 8) summary: (id: 7 priority: 0) summary: (id: 8 prior Ity: 2) summary: (id: 9 priority: 7) summary: (id: 10 priority: 8) summary: (id: 11 priority: 8) summary: (id: 12 priority: 1) summary: (id: 13 priority: 9) summary: (id: 14 priority: 9) summary: (id: 15 priority: 8) summary: (id: 16 priority: 8) summary: (id: 17 priority: 1) summary: (id: 18 priority: 0) summary: (id: 19 priority: 8) summary: (id: 20 priority: 10) summary: (id: 21 priority: 10) summary: (id: 2 2 priority: 10) summary: (id: 23 priority: 10) summary: (id: 24 priority: 10) summary: (id: 25 priority: 10) summary: (id: 26 priority: 10) summary: (id: 27 priority: 10) summary: (id: 28 priority: 10) summary: (id: 29 priority: 10) summary: (id: 30 priority: 0) summary: (id: 31 priority: 1) summary: (id: 32 priority: 2) summary: (id: 33 priority: 3) summary: (id: 34 priority: 4) summary: (id: 35 priority: 5) su Mmary summary: (id: 36 priority: 6) summary: (id: 37 priority: 7) summary: (id: 38 priority: 8) summary: (id: 39 priority: 9) summary: (id: 40 priority:-1) [-1] = Task> id: 40 calling shutdownNow the completion of prioritizedTaskConsumer * // *** is the same as the previous example. The creation sequence of the prioritizedTask object is recorded in the sequence list for comparison with the actual execution sequence. * The run () method slews for a short random period of time and then prints the object information. EndSentinel provides the same function as the previous one. * Make sure it is the last object in the queue. ** PrioritizedTaskProducer and PrioritizedTaskComsumer are connected to each other through PriorityBlockingQueue. * Because the blocking feature of this queue provides all the necessary synchronization, you should note that no explicit synchronization is required here-you do not have to consider reading from this queue *, is there any element in the queue, because when there is no element in the queue, the reader will be blocked directly. ***/