Menu
Question Index
...

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;
    }
}


1 10 2 35
35 10 2 1
1 2 10 35
Some other output
Compilation Error or Runtime Error

Doubts

Problems

Topic: Other Advanced Topics In Java

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App