Created
August 16, 2012 07:48
-
-
Save nurkiewicz/3368182 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.concurrent.DelayQueue; | |
import java.util.concurrent.Delayed; | |
import java.util.concurrent.TimeUnit; | |
import static java.lang.System.currentTimeMillis; | |
import static org.apache.commons.lang.time.DateUtils.MILLIS_PER_SECOND; | |
public class DelayQueueMain { | |
public static void main(String[] args) throws InterruptedException { | |
DelayQueue<Conference> conferences = new DelayQueue<Conference>(); | |
conferences.add(new Conference(5)); | |
conferences.add(new Conference(10)); | |
conferences.add(new Conference(15)); | |
while(!conferences.isEmpty()) { | |
System.out.println("Waiting for conference..."); | |
System.out.println(conferences.poll(1, TimeUnit.MINUTES)); | |
} | |
} | |
} | |
class Conference implements Delayed { | |
private long startTime; | |
public Conference(long delayInSeconds) { | |
this.startTime = currentTimeMillis() + delayInSeconds * MILLIS_PER_SECOND; | |
} | |
@Override | |
public int compareTo(Delayed o) { | |
Conference c = (Conference) o; | |
return startTime < c.startTime ? -1 : startTime == c.startTime ? 0 : 1; | |
} | |
@Override | |
public long getDelay(TimeUnit unit) { | |
return startTime - currentTimeMillis(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment