用于寻找楼层的电梯模拟中存在错误的逻辑

时间:2013-12-24 15:24:46

标签: java multithreading loops concurrency sleep

所以我想弄清楚电梯如何在一个实例中工作。它获得了一个人到达楼层及其目的地,并成功移动到达楼层。但是我不太确定如何正常工作。 它的意思是等待另一个Person被添加到Call队列中,但它只是无法正常工作。

这是电梯类中的run()方法

@Override
public synchronized void run()
{
    while(true)
    {


        List<ElevatorCall> callsWaiting = new ArrayList<ElevatorCall>();

        queue.drainTo(callsWaiting);

        for(int i = 0; i < callsWaiting.size(); i++)
        {   
            boolean directionToGo = getDirection(callsWaiting.get(i).getDestinationFloor(), currentPosition);
            int floorsToGoForCollection = getFloorsToGo(callsWaiting.get(i).getCollectionFloor());
            int floorsToGoForDestination = getFloorsToGo(callsWaiting.get(i).getDestinationFloor());

            //System.out.println("Elevator is moving to floor #" + callsWaiting.get(i).getCollectionFloor());

            System.out.println("Elevator is on floor " + currentPosition);

            for(int j = 0; j < floorsToGoForCollection + 1; j++)
            {
                boolean wasPersonOnFloor = checkIfPersonIsOnFloor(currentPosition, callsWaiting);
                boolean isDestinationFloor = checkIfDestination(currentPosition, currentElevatorCalls);

                if(wasPersonOnFloor)
                {
                    currentElevatorCalls.add(callsWaiting.get(floorsToGoForCollection - j));
                    System.out.println("A person on this floor called the elevator");

                    //floorsToGoForCollection = getFloorsToGo(callsWaiting.get(i).getDestinationFloor());
                    //j = 0;

                    directionToGo = getDirection(callsWaiting.get(i).getDestinationFloor(), currentPosition);
                    break;
                }

                if(isDestinationFloor)
                    callsWaiting = removeDestinationFromQueue(currentPosition, callsWaiting);

                if(callsWaiting.size() == 0)
                {
                    try {
                        wait();
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

                //floorsToGoForCollection = getFloorsToGo(callsWaiting.get(i).getCollectionFloor());
                //floorsToGoForDestination = getFloorsToGo(callsWaiting.get(i).getDestinationFloor());

                wasPersonOnFloor = false;
                moveFloor(directionToGo);

            }   
        }       
    }
}

在我的主要课程中,我有这个代码来模拟它。 我很确定我在run()中的逻辑是不正确的,但我似乎无法弄清楚它的哪个方面。

    run();

    Elevator elevator = new Elevator(queue);
    elevator.run();
}

public synchronized static void run()
{
    Random rand = new Random();
    int waitTime = rand.nextInt((1000 - 100) + 1) + 100;
    int startingID = 1;

    List<Person> people = new ArrayList<Person>(startingID);

    people.add(new Person(queue));

    for(int i = 0; i < people.size(); i++)
    {
        people.get(i).run();
    }

    try {
        Thread.sleep(waitTime);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

感谢您对逻辑的任何帮助。

1 个答案:

答案 0 :(得分:2)

您的Elevator班级或Person是否会延长Thread?请注意,要开始Thread,您必须拨打start()而不是run()。如果它是Runnable,则必须将其传递给新的Thread对象并start()该新线程。也许你的代码没有推进,因为它被困在一个循环中,因为它是在一个线程中处理的?