missing_mjj 2015-07-25 16:18 采纳率: 100%
浏览 1289
已采纳

帮忙看看这个程序的输出?

package thread.forkjoinpool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.TimeUnit;

public class Main3 {

static class Event implements Comparable<Event>{

    private int id;

    private int priority;

    /**
     * @param id
     * @param priority
     */
    public Event(int id, int priority) {
        this.id = id;
        this.priority = priority;
    }

    public int compareTo(Event o) {
        if(o==null) throw new IllegalArgumentException();
        if(this.priority>o.priority) return 1;
        else if(this.priority<o.priority) return -1;
        return 0;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "Event [id=" + id + ", priority=" + priority + "]";
    }

}

static class Run implements Runnable{

    private PriorityBlockingQueue<Event> queue;

    /**
     * @param queue
     */
    public Run(PriorityBlockingQueue<Event> queue) {
        this.queue = queue;
    }

    public void run() {

        for (int i = 10; i > 0; i--) {
            Event event = new Event(i, i);
            queue.add(event);
        }
    }

}


public static void main(String[] args) {

    PriorityBlockingQueue<Event> queue = new PriorityBlockingQueue<Event>();

    ExecutorService newCachedThreadPool = Executors.newCachedThreadPool();

    Run run = new Run(queue);

    newCachedThreadPool.execute(run);

    newCachedThreadPool.shutdown();

    try {
        boolean awaitTermination = newCachedThreadPool.awaitTermination(10, TimeUnit.SECONDS);
        if(awaitTermination){
            System.out.println("任务结束");
        }
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    System.out.println(queue);

    for (int i = 0; i < queue.size(); i++) {
        System.out.println(queue.size());
        Event poll = queue.poll();
        System.out.println(queue.size());
        System.out.println("结果:"+poll);
    }


}

}

任务结束
[Event [id=1, priority=1], Event [id=2, priority=2], Event [id=5, priority=5], Event [id=4, priority=4], Event [id=3, priority=3], Event [id=9, priority=9], Event [id=6, priority=6], Event [id=10, priority=10], Event [id=7, priority=7], Event [id=8, priority=8]]
10
9
结果:Event [id=1, priority=1]
9
8
结果:Event [id=2, priority=2]
8
7
结果:Event [id=3, priority=3]
7
6
结果:Event [id=4, priority=4]
6
5
结果:Event [id=5, priority=5]

这个为什么只输出了5个记录?但是集合的大小是对的

  • 写回答

2条回答 默认 最新

  • danielinbiti 2015-07-25 17:12
    关注
     for (int i = 0; i < queue.size(); i++) {
            System.out.println(queue.size());
            Event poll = queue.poll();//每次poll一个,queue长度在不断改变,当移除5个,的时候queue.size()就只有5了,而这时候i也是5,for条件(i<queue.size())不成立,退出循环,所以就移除了5个,也就是一半
            System.out.println(queue.size());
            System.out.println("结果:"+poll);
        }
    
        改成
        for (int i = queue.size()-1; i >=0; i--) {
            System.out.println(queue.size());
            Event poll = queue.poll();
            System.out.println(queue.size());
            System.out.println("结果:"+poll);
        }
            或者
            while(queue.size()>0){
            System.out.println(queue.size());
            Event poll = queue.poll();
            System.out.println(queue.size());
            System.out.println("结果:"+poll);
        }
    
            如果有帮助,记得采纳一下哦
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀