Results 1 to 2 of 2

Thread: DelayQueue poll(long timeout, TimeUnit unit) not working

  1. #1

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    DelayQueue poll(long timeout, TimeUnit unit) not working

    Im trying to test out using a DelayQueue but the poll method dosen't seem to be working properly. The docs say...
    E poll(long timeout, TimeUnit unit)

    Retrieves and removes the head of this queue, waiting if necessary up to the specified wait time if no elements with an unexpired delay are present on this queue.
    The specified wait time is one second and the elements are delayed a lot longer than that.
    Code:
    import java.util.concurrent.Delayed;
    import java.util.concurrent.TimeUnit; 
    import java.util.concurrent.DelayQueue; 
    
    public class DelayTest{
     public static void main(String[] args){
      DelayQueue<DelayedElement> dq = new DelayQueue<DelayedElement>();
      dq.put(new DelayedElement(32,"32"));
      dq.put(new DelayedElement(25,"25"));
      dq.put(new DelayedElement(15,"15"));
     try{
      DelayedElement de = dq.poll(1, TimeUnit.SECONDS); 
       System.out.println(de.getDelayTime()); 
     }catch(InterruptedException ie){
      System.err.println(ie);
      }
     }
    }
    
    class DelayedElement implements Delayed, Comparable<Delayed>{
     private long sec;
     private String delay;  
    
     public long getDelay(TimeUnit unit){
      long d = unit.convert(sec - (System.currentTimeMillis() / 1000), TimeUnit.SECONDS);
      return d;
     }
     
     public DelayedElement(long sec, String delay){
      this.sec = sec; 
      this.delay = delay; 
     }
     public String getDelayTime(){
        return delay; 
     }
     
     public int compareTo(Delayed other){
      if(other == this) 
       return 0;
       DelayedElement x = (DelayedElement)other;
       long diff = sec - x.sec;
       if(diff < 0)
        return -1;
       else if(diff > 0)
        return 1;
       else return 1; 
       }
     }

  2. #2

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: DelayQueue poll(long timeout, TimeUnit unit) not working

    Scrap that last question. I made some mods to the code and I switched to using take() which blocks. Problem is that take() is blocking forever. Can anyone see what I am doing wrong?
    Code:
    import java.util.concurrent.Delayed;
    import java.util.concurrent.TimeUnit; 
    import java.util.concurrent.DelayQueue; 
    
    public class DelayTest{
     public static void main(String[] args){
      DelayQueue<DelayedElement> dq = new DelayQueue<DelayedElement>();
      dq.put(new DelayedElement(10));
      dq.put(new DelayedElement(20));
      dq.put(new DelayedElement(5));
    
     while(true){
      try{  
      // DelayedElement de = dq.poll(); // if this queue has no elements with an unexpired delay no blocking   
       DelayedElement de = dq.take(); // waits for an expired element then remove head of queue
       if(de == null) break; 
        System.out.println(de.getDelayTime());
       }catch(InterruptedException ie){
       System.err.println(ie);
       }
      }
     }
    }
    
    class DelayedElement implements Delayed, Comparable<Delayed>{
     private long delay;
     
     public long getDelay(TimeUnit unit){
      long d = unit.convert(delay, TimeUnit.MILLISECONDS);
      return d;
     }
     
     public DelayedElement(long delay){
      this.delay = delay; 
     }
    
     public long getDelayTime(){
      return delay; 
     }
     
     public int compareTo(Delayed other){
      if(other == this) 
       return 0;
       DelayedElement x = (DelayedElement)other;
       long diff = delay - x.delay;
       if(diff < 0){
        return -1;
       }
        return 1;  // diff > 0
     }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width