DelayQueue method put() in Java
A DelayQueue method put() inserts the specified element into this priority deque. As the deque is unbounded, this method will never block.
Syntax
 1 public void put(E e)
e : It is an element to be inserted in to the priority deque.
DelayQueue method put()
 1 package com.java;
 2 
 3 import java.util.Arrays;
 4 import java.util.concurrent.DelayQueue;
 5 
 6 public class Main {
 7 
 8     public static void main(String[] args) {
 9 
 10         DelayQueue<DelayInteger> deque = new DelayQueue<>();
 11         DelayInteger di1 = new DelayInteger(120, 1);
 12         DelayInteger di2 = new DelayInteger(144, 2);
 13         DelayInteger di3 = new DelayInteger(160, 3);
 14 
 15         deque.addAll(Arrays.asList(di1, di2));
 16         System.out.println("A DelayQueue elements : " + deque);
 17 
 18         // add new element
 19         deque.put(di3);
 20         System.out.println("A DelayQueue elements : " + deque);
 21     }
 22 }
In the above example, a DelayQueue of type DelayInteger created and added elements using addAll() method by passing a collection. A put() method called by passing an object that inserts specified object at the end of this deque and print updated deque.
Output
 1 A DelayQueue elements : [120 -> 1723724547133, 144 -> 1723724547134]
 2 A DelayQueue elements : [120 -> 1723724547133, 144 -> 1723724547134, 160 -> 1723724547135]
DelayInteger class
 1 package com.java;
 2 
 3 import java.util.concurrent.Delayed;
 4 import java.util.concurrent.TimeUnit;
 5 
 6 public class DelayInteger implements Delayed {
 7     private Integer value;
 8     private Long time;
 9 
 10     DelayInteger(Integer value, long time) {
 11         this.value = value;
 12         this.time = System.currentTimeMillis() + time;
 13     }
 14 
 15     @Override
 16     public long getDelay(TimeUnit unit) {
 17         long diff = time - System.currentTimeMillis();
 18         return unit.convert(diff, TimeUnit.SECONDS);
 19     }
 20 
 21     public Integer getValue() {
 22         return this.value;
 23     }
 24 
 25     @Override
 26     public int compareTo(Delayed o) {
 27         Long val = ((DelayInteger)o).time;
 28         if(val == null || this.time == null) {
 29             return 0;
 30         } else {
 31             return (int)(this.time - val);
 32         }
 33     }
 34 
 35     @Override
 36     public String toString() {
 37         return this.value + " -> " + this.time;
 38     }
 39 }

DelayQueue method put() with String

DelayQueue method put() with String
 1 package com.java;
 2 
 3 import java.util.Arrays;
 4 import java.util.concurrent.DelayQueue;
 5 
 6 public class StringExp {
 7 
 8     public static void main(String[] args) {
 9 
 10         DelayQueue<DelayString> deque = new DelayQueue<>();
 11         DelayString ds1 = new DelayString("Welcome", 1);
 12         DelayString ds2 = new DelayString("to", 2);
 13         DelayString ds3 = new DelayString("IOGyan", 3);
 14 
 15         deque.addAll(Arrays.asList(ds1, ds2));
 16         System.out.println("A DelayQueue elements : " + deque);
 17 
 18         // add new element
 19         deque.put(ds3);
 20         System.out.println("A DelayQueue elements : " + deque);
 21     }
 22 }
In the above example, a DelayQueue of type DelayString created and added elements using addAll() method by passing a collection. A put() method called by passing an object that inserts specified object at the end of this deque. As new object added to this deque, it returns boolean value true and print updated deque.
Output
 1 A DelayQueue elements : [Welcome -> 1723724608582, to -> 1723724608583]
 2 A DelayQueue elements : [Welcome -> 1723724608582, to -> 1723724608583, IOGyan -> 1723724608584]
DelayString class
 1 package com.java;
 2 
 3 import java.util.concurrent.Delayed;
 4 import java.util.concurrent.TimeUnit;
 5 
 6 public class DelayString implements Delayed {
 7     private String name;
 8     private Long time;
 9 
 10     DelayString(String name, long time) {
 11         this.name = name;
 12         this.time = System.currentTimeMillis() + time;
 13     }
 14 
 15     public String getName() {
 16         return this.name;
 17     }
 18 
 19     @Override
 20     public long getDelay(TimeUnit unit) {
 21         long diff = time - System.currentTimeMillis();
 22         return unit.convert(diff, TimeUnit.SECONDS);
 23     }
 24 
 25     @Override
 26     public int compareTo(Delayed o) {
 27         Long val = ((DelayString)o).time;
 28         if(val == null || this.time == null) {
 29             return 0;
 30         } else {
 31             return (int)(this.time - val);
 32         }
 33     }
 34 
 35     @Override
 36     public String toString() {
 37         return this.name + " -> " + this.time;
 38     }
 39 }

DelayQueue method put() with custom object

DelayQueue method put() with custom object
 1 package com.java;
 2 
 3 import java.util.Arrays;
 4 import java.util.concurrent.DelayQueue;
 5 
 6 public class Example {
 7 
 8     public static void main(String[] args) {
 9 
 10         Product prd1 = new Product(1, "Phone", 2, 50000);
 11         Product prd2 = new Product(2, "Monitor", 2, 23000);
 12         Product prd3 = new Product(3, "Laptop", 2, 130000);
 13 
 14         DelayProduct dp1 = new DelayProduct(prd1, 1);
 15         DelayProduct dp2 = new DelayProduct(prd2, 2);
 16         DelayProduct dp3 = new DelayProduct(prd3, 3);
 17 
 18         DelayQueue<DelayProduct> deque = new DelayQueue<>();
 19         deque.addAll(Arrays.asList(dp1, dp2));
 20         System.out.println("A DelayQueue elements : " + deque);
 21 
 22         // add new element
 23         deque.put(dp3);
 24         System.out.println("A DelayQueue elements : " + deque);
 25     }
 26 }
In the above example, a DelayQueue of type Employee created and added elements using addAll() method by passing a collection. A put() method called by passing an Employee that inserts value at the end of this deque. As new object added to this deque, it returns boolean value true and print updated deque.
Output
 1 A DelayQueue elements : [Phone 2 50000.0 -> 1723724677533, Monitor 2 23000.0 -> 1723724677534]
 2 A DelayQueue elements : [Phone 2 50000.0 -> 1723724677533,
 3  Monitor 2 23000.0 -> 1723724677534, Laptop 2 130000.0 -> 1723724677535]
DelayProduct class
 1 package com.java;
 2 
 3 import java.util.concurrent.Delayed;
 4 import java.util.concurrent.TimeUnit;
 5 
 6 public class DelayProduct implements Delayed {
 7     private Product product;
 8     private Long time;
 9 
 10     DelayProduct(Product product, long time) {
 11         this.product = product;
 12         this.time = System.currentTimeMillis() + time;
 13     }
 14 
 15     public Product getProduct() {
 16         return this.product;
 17     }
 18 
 19     @Override
 20     public long getDelay(TimeUnit unit) {
 21         long diff = time - System.currentTimeMillis();
 22         return unit.convert(diff, TimeUnit.SECONDS);
 23     }
 24 
 25     @Override
 26     public int compareTo(Delayed o) {
 27         Long val = ((DelayProduct)o).time;
 28         if(val == null || this.time == null) {
 29             return 0;
 30         } else {
 31             return (int)(this.time - val);
 32         }
 33     }
 34 
 35     @Override
 36     public String toString() {
 37         return this.product + " -> " + this.time;
 38     }
 39 }

Product class

Product class
 1 package com.java;
 2 
 3 public class Product {
 4     private final int pid;
 5     private final String name;
 6     private final long quantity;
 7 
 8     private final double price;
 9 
 10     public Product(int pid, String name, long quantity, double price) {
 11         this.pid = pid;
 12         this.name = name;
 13         this.quantity = quantity;
 14         this.price = price;
 15     }
 16 
 17     public int getPid() {
 18         return pid;
 19     }
 20 
 21     public String getName() {
 22         return name;
 23     }
 24 
 25     public long getQuantity() {
 26         return quantity;
 27     }
 28 
 29     public double getPrice() { return price; }
 30 
 31     @Override
 32     public String toString() {
 33         return this.name + " " + this.quantity + " " + this.price;
 34     }
 35 }
Privacy Policy
Terms of Service
Disclaimer
Contact us
About us