【问题标题】:Refilling a BlockedQueue in Java在 Java 中重新填充 BlockedQueue
【发布时间】:2013-12-25 09:55:27
【问题描述】:

所以我有这个循环

public void run()
{
    while(true)
    {
        //Look at everything on queue currently and pulls them into a list
        List<ElevatorCall> callsWaiting = new ArrayList<ElevatorCall>();
        queue.drainTo(callsWaiting);

        ElevatorCall nextDestination = getBestOption(callsWaiting);


        System.out.println("Picked Person(" + nextDestination.getPerson().getId() + ") as best option");

        //Work on this section
        if(nextDestination != null)
        {
            sortCallsByShortestDistanceAndDirection(workQueue, nextDestination.getDirection());

            boolean arrived = moveTowards(nextDestination);

            while(!arrived)
            {
                //Check for new calls
                List<ElevatorCall> newCalls = new ArrayList<ElevatorCall>();
                queue.drainTo(newCalls);

                checkFloorForCallsAndAddToElevator(workQueue);
                sortCallsByShortestDistanceAndDirection(newCalls, nextDestination.getDirection());

                arrived = moveTowards(nextDestination);
            }
            arrived = false;
        }
    }
}

我希望它继续四处走动并找到目标楼层。但是,当它找到第一个目标楼层时,循环结束,因为当它回到顶部时, queue.drainTo(callsWaiting) 不起作用,因为队列已经被清空。我想在我设置为 false 后我需要重新填充它,但是我不知道该怎么做。 感谢您对此的任何帮助。

【问题讨论】:

    标签: java multithreading concurrency queue fill


    【解决方案1】:

    我不确定您的 getBestoption 是做什么的。如果队列中有 3 个请求(7 楼、8 楼、20 楼),则在进行 while 循环之前。

    while(true)
    {
        //Look at everything on queue currently and pulls them into a list
        List<ElevatorCall> callsWaiting = new ArrayList<ElevatorCall>();
        queue.drainTo(callsWaiting);
    
        ElevatorCall nextDestination = getBestOption(callsWaiting);
    


    在上面的部分中,您正在检索所有请求并放置在呼叫等待中(呼叫等待中的 3 个 ElevatorCall 对象。当您获得 nextDestination 时(如果您当前的电梯位于 5 楼,我假设您选择了 7 楼并移除 7 楼)呼叫等待中的楼层请求。然后通过剩余线路处理第 7 层请求。但是您何时/如何处理呼叫等待中的其他两个请求?
    而不是 if 条件

     //Work on this section
        if(nextDestination != null)
    


    那里有循环,会解决你的问题。喜欢

    while (nextDestination != null) {
           sortCallsByShortestDistanceAndDirection(workQueue, nextDestination.getDirection());
    
            boolean arrived = moveTowards(nextDestination);
    
            while(!arrived)
            {
                //Check for new calls
                List<ElevatorCall> newCalls = new ArrayList<ElevatorCall>();
                queue.drainTo(newCalls);
    
                checkFloorForCallsAndAddToElevator(workQueue);
                sortCallsByShortestDistanceAndDirection(newCalls, nextDestination.getDirection());
    
                arrived = moveTowards(nextDestination);
            }
            arrived = false;
            nextDestination = getBestOption(callsWaiting);    
    

    }

    注意**,从 getBestoption 中选择 nextDestination,如果传递的集合为空,则假定 getBestoption 返回 null。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-07
      • 1970-01-01
      • 2016-09-15
      相关资源
      最近更新 更多