Menu
Topics Index
...
`

Creating DelayQueue And Iterator for it


If you need explanation Read this topic

If you need Answer Take test on this topic

Creating DelayQueue And Iterator for it
What will be the output of the following program?
import java.util.*;
import java.util.concurrent.*;

public class SlowTest {
    public static void main(String... args) {
        DelayQueue dq = new DelayQueue();
        DelayTest obj1 = new DelayTest(10);
        DelayTest ob2 = new DelayTest(2);
        DelayTest ob3 = new DelayTest(1);
        DelayTest ob4 = new DelayTest(35);
        dq.offer(obj1);
        dq.put(ob2);
        dq.offer(ob3);
        dq.add(ob4);
        Iterator itr = dq.iterator();
        while (itr.hasNext()) {
            DelayTest dt = (DelayTest) itr.next();
            System.out.print(dt.deleyTime + " ");
            itr.remove();
        }
    }
}

class DelayTest implements Delayed {
    public long deleyTime = 0;
    DelayTest(long deleyTime) {
        this.deleyTime = deleyTime;
    }
    public int compareTo(Delayed ob) {
        if (deleyTime < ((DelayTest) ob).deleyTime)
        return -1;
        else if (deleyTime > ((DelayTest) ob).deleyTime)
        return 1;
        return 0;
    }
    public long getDelay(TimeUnit unit) {
        unit.convert(deleyTime - System.currentTimeMillis(), TimeUnit.NANOSECONDS);
        return 0;
    }
}

A. 1 10 2 35
B. 35 10 2 1
C. 1 2 10 35
D. Some other output
E. Compilation Error or Runtime Error
Topic: Other Advanced Topics In Java

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

ans is A..compareTo method implemented according to their delayed times..so the obj arranged in such a order

Posted by Uday Kumar    2014-06-19 13:31:25


ans a. if return value of compareto methd 1 thn objects are placd in same locatn.
if it return -1 thn objcts are swapd.
opertn follws:
10
2 10  
1 10 2  
1 10  2 35

Posted by Maheshwari Natarajan    2014-06-19 14:11:41


Congratulations Maheshwari Natarajan. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-06-20 04:32:31


© meritcampus 2019

All Rights Reserved.

Open In App