java - 如何根据元素属性从 PriorityQueue 中删除元素?

标签 java search priority-queue

我尝试在我的 PQueue 中设置 Maximum Waiting Time。如果有任何链接等待超过Maximum Waiting Time,这个Maximum Waiting Time会自动检查我的PQueue去掉它。我对我的代码进行了此更改,它正在运行,但它在删除链接后正好停止。我想根据等待时间条件从我的 PQueue 中删除所有元素。你能告诉我这里缺少什么吗?

这是我的课:

public class MyClass {

    public static PriorityQueue <LinkNodeLight> PQueue = new PriorityQueue <> (); 


    private static Set<String> DuplicationLinksHub = new LinkedHashSet <> ();         

    private static Integer IntraLinkCount = new Integer (0);                 
    private static Integer InterLinkCount = new Integer (0);                 
    private static Integer DuplicationLinksCount = new Integer (0);     
    private static Integer MaxWaitTime = new Integer (60000); // 1 M= 60000 MS


    @SuppressWarnings("null")
    LinkNode deque(){

        LinkNode link = null;
        synchronized (PQueue) {

            link = (LinkNode) PQueue.poll();
            if (link != null) {
                link.setDequeTime(new DateTime());
                if (link.isInterLinks())
                    synchronized (InterLinkCount) {
                        InterLinkCount--;
                        }
                else
                    synchronized (IntraLinkCount) {
                        IntraLinkCount--;
                        }
            }

            synchronized (PQueue) {
                if (link.waitingInQueue()>MaxWaitTime) {

                    link = (LinkNode) PQueue.remove();
                                    System.out.println("*********************************");
                                    System.out.println("This Link is Deopped: " + link);
                                    System.out.println("%%% MaX Waiting Time:" + (MaxWaitTime/60000)+"Min");

                                    System.out.println("*********************************");
                  }
            }
            return link;


        }

最佳答案

你的问题有点不透明,但如果我理解正确的话,你想检查你的 PriorityQueue 看看是否有等待时间超过特定时间的项目。

如前所述,您对 IntraLinkCountInterLinkCountsynchronized 使用有点奇怪。有一个相当未知的替代方案,原子整数类 AtomicInteger(在包 java.util.concurrent.atomic 中:

private static AtomicInteger IntraLinkCount = Integer.valueOf(0);

这将如您所愿。

第二个问题是您使用了poll() 方法。这将从队列中删除 顶部项目。也许您想改用 peek(),然后仅在返回的链接对象满足 link.waitingInQueue() > MaxWaitTime 时才使用 remove() >?

顺便说一句,您的队列将根据它们的“自然顺序”返回项目。这意味着使用了 compareTo 方法,“最小的”将首先从队列中返回。我认为您可能想要实现一个自定义的 compareTo,将 最长等待 链接放在第一位?

你也可以 create your PriorityQueue with a custom Comparator对象代替。

像这样:

public class MyClass {
    public static PriorityQueue<LinkNodeLight> PQueue = new PriorityQueue<>();

    private static AtomicInteger IntraLinkCount = new AtomicInteger(0);
    private static AtomicInteger InterLinkCount = new AtomicInteger(0);

    private static Integer MaxWaitTime = Integer.valueOf(60_000); // 1 M= 60000 MS

    LinkNode deque() {
        LinkNode link = null;

        synchronized (PQueue) {
            link = PQueue.peek();

            if (link != null) {
                link.setDequeTime(LocalDateTime.now());

                if (link.isInterLinks())
                    InterLinkCount.decrementAndGet();
                else
                    IntraLinkCount.decrementAndGet();

                if (link.waitingInQueue() > MaxWaitTime) {
                    link = PQueue.remove();

                    System.out.println("*********************************");
                    System.out.println("This Link is Deopped: " + link);
                    System.out.println("%%% MaX Waiting Time:" + MaxWaitTime / 60000 + "Min");
                    System.out.println("*********************************");

                    return link;
                } else
                    return null;
            }
        }

        return link; // Not sure what you want to return here
    }
}

如果你有幸使用 Java 8,像这样的魔法可能会有用:

synchronized (PQueue) {
    link = PQueue.stream().filter(node -> node.waitingInQueue() > MaxWaitTime).findFirst().orElse(null);

    if (link != null)
        PQueue.remove(link);
}

关于java - 如何根据元素属性从 PriorityQueue 中删除元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33301146/

相关文章:

java - 如何遍历到完整树的末尾?

java - 当添加一些随机元素时,PriorityQueue中的优先级如何确定?

java - Xalan 的 SAX 实现 - 字符串中的双重编码实体

javascript - 如何在 Object 中搜索在数组中也有值的值?

java - 存储数组 - MYSQL

arrays - Excel VBA 搜索大数组

java - 在包含特定项目名称的类的数组列表中搜索

c - 如何在作为二叉堆实现的优先级队列中保留相同优先级元素的顺序?

java - sqlite android中的编码字符

java - jackson 未能将字符串反序列化为 Joda-Time