BUAA_ OO_ Unit 2

Time:2020-8-14

BUAA_OO_2020_UNIT2

1、 Program structure analysis

  • The fifth assignment

    UML & Mertrics

    BUAA_ OO_ Unit 2

    BUAA_ OO_ Unit 2BUAA_ OO_ Unit 2

    The elevator scheduling problem is essentially a task request and assignment problem. In the fifth operation, the author uses a simple “producer consumer” model to establish a modelDinThe thread as a producer parses the input and adds a transport request to theElevThread output, data to be processed by the main control classCtrlMaintenance, and as an elevator “scheduler”, the first two operations are thread safeCtrlResponsible. The last two jobs also continue this architecture. The fact is that we can cope with this iteration without refactoring. However, the author requests the integration of queue and scheduler, and there is only one level of scheduling, which leads to the abnormal overstaffing of the main control class.

  • The sixth assignment

    UML & Mertrics

    BUAA_ OO_ Unit 2

    BUAA_ OO_ Unit 2BUAA_ OO_ Unit 2

    It is still the same as the old three, the sixth job expansion function is relatively easy to achieve, carry out the method expansion of related classes, mainly to achieve multi elevator threadCtrlClass processing is not very good, next time rewriteCtrl

  • The seventh assignment

    UML & Mertrics

    BUAA_ OO_ Unit 2

    BUAA_ OO_ Unit 2BUAA_ OO_ Unit 2

    In order to realize the transfer function, the author established theMyReqStorage classPersonRequstWith the passenger’s current building number, andProcess orientedAccording to the transfer map, the floor mapping mechanism is established for each type of elevator

2、 Bug analysis

  • In the fifth job, we mainly encountered the thread scheduling bug of multithreading, which was mainly because the author didn’t find the precise one at that timenotify()The condition, actually arousesRUNNALBEBecause ofCtrlIs shared, resulting in the input being blocked by the elevator thread, but finally determined to enterwait()The conditions of.
  • In the sixth operation, the author realized the importance of thread safety in this unit. When dealing with the input of elevator number, the author didn’t use the input documentElevatorInput.getElevatorNum()Instead of using(new Scanner(System.in)).nextInt(),System.inInput interface andScannerSharing, resulting in input buffer thread unsafe, the author really pulled the crotch this time, directly give pleasure away
  • Another vicious bug appeared in the seventh assignmentCThe passengers on the elevator want to go upstairs4The author sent it to the building directly3, Dan Lou3onlyCCan go,BCan’t receive, this strong test is almost overturned, but the overall structure of the author of the three assignments is still OK, through strict controlsynchronizedIn addition to these bugs, there are no deadlocks or data adventure bugs that are hard to reproduce

3、 Mutual testing strategy

  • The fifth operation function is relatively simple. As long as the simple thread scheduling is ensured, only the single elevator scheduling problem is considered. You can input 30 pieces of data from the ground floor to the top floor to test whether others have completed the piggyback
  • The sixth mutual test was not carried out, but the author thinks that we can carry out hack from the two aspects of elevator overload and the maximum number of input instructions
  • The seventh time focuses on thread safety and transfer. You can input only one command to transfer to test whether the relevant elevator thread has exited before the transfer is completed. It can also observe the definition domain of three types of elevator building number around the transfer problem1,3,15It is the most special three transfer points. It can also enumerate all the requests and exclude the direct request for transfer

4、 Object creation mode

  • The fifth job creates only the input threadDin, elevator threadElevI didn’t expect to see some friends turning the scheduler into threads,DinResponsible toCtrlInput data and wake up all data after input and endWAITINGOfElevElevThe end condition of is that there are no pending requests andDinAt the end, a global variable without input is set. In terms of specific scheduling strategy, theLOOKAlgorithm, but regardless ofSCANstillLOOK, all of them lost the request information in one direction. I always feel that it is not very good. So after that, the author really fragrantGREEDYFinally, a pure greedy elevator scheduling and elevator scheduling strategy is created

  • The sixth assignment did not change much, but mainly changedCtrlIn this paper, the author changed the single elevator to greedy elevator, and some students used the average distribution, random distribution to assign tasks to each elevator, but the author still used the elevator free greedy competition strategy. The reason is that there are two assumptionsElev, one empty, one carrying passengers, starting from the same building number, for a request, carrying peopleElevBecause of the request in the elevator, the average probability of stopping or turning is higher, and the empty load is more likely to seize the request. In fact, the less passengers carry, the more direct response can be made to the request outside the elevator, which effectively reduces the probability that some elevators have been idling, thus automatically realizing the priority task allocation and improving theElevThe parallel rate can be used by one person at a timeThread.sleep(5)Improve the parallel rate.There is no need to write secondary scheduling

  • The seventh operation, in view of the need to achieve transfer, it is necessary to maintain the current building number of transfer passengers, so the author built a new oneMyReqClass improvementPersonRequstFor the above2innotifyThe author also found a safe and convenient solution

    for (int i = 0; i < elevNum; i++) {
                if (elevs.get(i).getState().equals(Thread.State.valueOf("WAITING"))) {
                    synchronized (elevs.get(i)) {
                        elevs.get(i).notifyAll();
                    }
                }
            }//First judge whether it is in wait() and then notifyall();

    At the same time, pay attention to the thread safety of the pit, the author is to ensure that allElevLast oneDEADTo prevent the elevator from leaving early

    After solving the thread design, the troublesome transfer problem comes. The floor number definition domain of each type of elevatorA = [-3, 1] ∪ [15, 20],B = [-2, 15] - {3}C = [1, 8] * 2 - 1, find the transfer point(A ∩ B) ∪ (A ∩ C) ∪ (B ∩ C) = (C - {3}) ∪ {-1, -2}The author does not want to write the secondary scheduling, so the address translation is establishedElevYesMyReqWe just need to be in the elevatorA-CFinish nextMyReqIn the elevator and in the elevator outside the building number conversion line, actually complete the former line, if the elevator insidegetToFloor()If it is illegal, the destination will be changed to the transfer point with the shortest transfer path. Outside the elevator, first assume the elevator. After the conversion, if the target is the current floor, the original destination will be retained. Otherwise, you can go to the elevator directly. The author is really lazy to death, this kind of specific situation problems or the best programming table, do not like the author lost in details.

5、 Experience

The author’s work effect in this unit is not ideal, or the understanding of thread safety is not deep, as well as the lack of attention to the details of multi-threaded programming, which leads to bugs. The author still resents his own scheduler. The author should realize the separation of queue and scheduling. Such scheduler coupling is too high.