Home  >  Core Java

Examples of DelayQueue in Java

By Arvind Rai, December 16, 2012
1. DelayQueue is an unbounded queue. It extends Delayed interface.
2. Element from DelayQueue can only be taken when its delay has expired.
3. At the head of the queue , element with furthest expired delay time is found.
4. An element is expired when its getDelay() method returns a value less than or equal to zero.
5. Null is not permitted in DelayQueue.
6. In DelayQueue, only those object can be inserted which implements Delayed interface.

Find the example of DelayedQueue. I have created a class DelayedTest extending Delayed interface. This will implement compareTo and getDelay() method
package com.concretepage;

import java.util.Iterator;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

public class DelayQueueTest  {
	public static void main(String... args){
		DelayQueue  dq=new DelayQueue();
		DeleyedTest ob1=new DeleyedTest(10);
		DeleyedTest ob2=new DeleyedTest(5);
		DeleyedTest ob3=new DeleyedTest(15);
		
		dq.offer(ob1);
		dq.offer(ob2);
		dq.offer(ob3);
		
		Iterator itr=dq.iterator();
		while(itr.hasNext()){
			DeleyedTest dt=(DeleyedTest)itr.next();
			System.out.println(dt.deleyTime);
		}
	}
}

class DeleyedTest implements Delayed{
    public long deleyTime=0;
    DeleyedTest(long deleyTime){
    	this.deleyTime=deleyTime;
    }
    
	@Override
	public int compareTo(Delayed ob) {
		if(this.deleyTime<((DeleyedTest)ob).deleyTime){
			return -1;
		}else if(this.deleyTime>((DeleyedTest)ob).deleyTime){
			return 1;
		}
		return 0;
	}

	@Override
	public long getDelay(TimeUnit unit) {
		unit.convert(deleyTime-System.currentTimeMillis(),TimeUnit.NANOSECONDS); 
		return 0;
	}
	
}
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS
comments powered by Disqus

Sitewide-Feb4of4234x60






Copyright ©2017 concretepage.com, all rights reserved |Privacy Policy | Contact Us