Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-5055158

DelayQueue take(), poll() and poll(long delay, TimeUnit tu) are flawed

    XMLWordPrintable

    Details

      Description

      DelayQueue take(), poll() and poll(long delay, TimeUnit tu) are flawed

      In case of poll() if getDelay(TimeUnit unit) returns > 0 it returns null.
      As per Spec it will return null in case queue is empty.

      In case of take() and Poll(long delay, TimeUnit unit) trying to get the element out of queue goes into infinite loop calling getDelay(TimeUnit unit), never comes out of it.

      Tried on Solaris-9 JDK build Tiger-Beta2-b51.

      With the same code one can try different methods like take() and poll() too.

      for take() and poll(long delay , TimeUnit unit) there is workaround, if one comments the StraightLogic and uncomment the Work Around it will work fine.

      Code:-

      import java.util.concurrent.*;



      public class Test2 {
        
        public void assertion1() throws Exception {
           DelayQueue<Fillers> queue = new DelayQueue<Fillers>();
           
           // add elements into queue
           for(int i =0; i<20; i++) {
             Fillers fill = new Fillers(1,2l);
             queue.add(fill);
            }
          
           if(queue.size() != 20) {
               System.out.println("queue does not have expected set of elements");
           } else {
                for(int i =0; i<20; i++) {
                   System.out.println("Element :"+queue.poll(1L,TimeUnit.SECONDS));
                }
           }
        }

        public static void main(String args[]) throws Exception {
           Test2 ref = new Test2();
           ref.assertion1();
        }

      }


      class Fillers implements Delayed {
            Integer k ;
            long t;
            TimeUnit tu = TimeUnit.SECONDS;
            private int count =0;
            
            public Fillers(int k, long time) {
              this.k = new Integer(k);
              this.t = time;
            }
            
            public long getDelay(TimeUnit ref) {
               System.out.println("Time unit passed is :"+ref);
               System.out.println("time: "+ ref.convert(t, tu));
               
               // Straight logic
               return ref.convert(t, tu);
               
               // work around
               /*
               if(count == 0) {
                  count++;
                  return ref.convert(t, tu);
               } else {
                  return 0l;
               }
               */
             
            }
            
            public int compareTo(Delayed ref) {
               Fillers ref1 = (Fillers)ref;
               System.out.println("Compare to is being called");
               if( this.t < ref1.t)
                   return -1;
               if(this.t == ref1.t) {
                   return 0;
                } else {
                  return 1;
                }
            }
        
        
        }

        Attachments

          Activity

            People

            Assignee:
            martin Martin Buchholz
            Reporter:
            jsinghsunw Jit Singh (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: