How do i set up a DelayQueue's Delay

前端 未结 4 888
梦如初夏
梦如初夏 2021-01-19 04:42

I\'m just starting out coding in java i\'m in struggling with setting up a DelayQueue,

I wanted to have it so,

DelayQueue queue = new DelayQueue();

         


        
4条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-19 04:50

    import java.util.concurrent.BlockingQueue;
    import java.util.concurrent.DelayQueue;
    import java.util.concurrent.Delayed;
    import java.util.concurrent.TimeUnit;
    
    public class DelayQueueExample {
    
        public static void main(String[] args) {
    
            BlockingQueue blockingQueue = new DelayQueue();
    
            try {
                blockingQueue
                        .put(new DelayedElement(4000, "Message with delay 4s"));
                blockingQueue
                        .put(new DelayedElement(2000, "Message with delay 2s"));
                blockingQueue
                        .put(new DelayedElement(9000, "Message with delay 9s"));
            } catch (InterruptedException ie) {
            }
    
            while (!blockingQueue.isEmpty()) {
                try {
                    System.out.println(">>" + blockingQueue.take());
                } catch (InterruptedException ie) {
                }
    
            }
    
        }
    }
    
    class DelayedElement implements Delayed {
    
        private long duration = 0;
        private String message;
    
        public DelayedElement(long duration, String name) {
            this.duration = System.currentTimeMillis() + duration;
            this.message = name;
        }
    
        @Override
        public int compareTo(Delayed o) {
            return (int) (this.duration - ((DelayedElement) o).getDuration());
        }
    
        @Override
        /*
         * Expiration occurs when an element's getDelay(TimeUnit unit) method
         * returns a value less than or equal to zero.
         */
        public long getDelay(TimeUnit unit) {
            long diff = duration - System.currentTimeMillis();
            return unit.convert(diff, TimeUnit.MILLISECONDS);
        }
    
        public long getDuration() {
            return duration;
        }
    
        public void setDuration(long duration) {
            this.duration = duration;
        }
    
        public String getMessage() {
            return message;
        }
    
        public void setMessage(String message) {
            this.message = message;
        }
    
        @Override
        public String toString() {
            return "DelayedElement [duration=" + duration + ", message=" + message
                    + "]";
        }
    
    }
    

提交回复
热议问题