OO Unit 2 Multi-thread Elevator Summary Review

Write in front

  This is the first time I have come into contact with multi-threaded programming. It will not be debugged. The factors that need to be considered when programming are increased. Many bugs are difficult to reproduce, which has become my biggest obstacle to this job. Looking at my own elevator while programming, I quietly determined that if I were to engage in similar work in the future, I would definitely not take the elevator I developed. But in this unit, I really have a lot of gains, and I can naturally think about the elevator scheduling algorithm when taking the elevator.

 

1. Design strategy

  The three operations in this unit follow the producer-consumer model. Because the input is relatively simple, the input is directly completed in the Main class, and the producer thread is not separately constructed. After inputting the instruction, the request as a product is passed into the tray (buffer) through the buffer's put () method, and the elevator as the consumer obtains the instruction from the tray through the get () method and runs. The scheduling function is implemented in the buffer. The elevator thread and the buffer thread are started and ended in the main thread, wherein the end of the elevator thread is assisted by an end signal end. In the buffer, the put and get methods need to ensure thread safety, implemented through synchronized and wait, notifyAll. The elevator obtains instructions from the buffer through get every cycle. If there are no instructions (or no instructions that require the current elevator), no one in the elevator, no elevator has not yet processed the request, and the program has not ended, the elevator process enter wait state. Whenever a new instruction is added or the program input ends, notifyAll.

 

2. Design scalability

  The work of this unit has fully learned the lesson of the first unit refactoring every time. In the first homework, I carefully thought about the architecture, which has greatly improved the scalability of the program. The three elevators all use the LOOK algorithm. The operation of a single elevator except for changing the upper limit of passengers and the running floor time due to the requirements of the problem, all the elevators of the three operations use the same model. The producer (ie main class) thread has barely changed. The scheduler changes a lot. The first, third, fifth and fifteenth floors are fixed transfer floors. If an instruction needs to be transferred, the transfer attribute of the instruction is set to true, and the TO floor is changed to one of the first, third, fifth and fifteenth. And a new instruction (that is, the transfer instruction after the instruction reaches the floor) is added. When the instruction reaches the transfer floor, the get method is called to add the instruction to the buffer.

  In the third operation, because the three types of ABC elevators use the same model, the dispatch of instructions is completely completed by the dispatcher, which makes the elevator functions more specific. When an instruction is put into the buffer, it will judge whether the elevator needs to be transferred and accept the instruction according to the From and To floors, and attach the corresponding status to it. This operation greatly improves the scalability.

SOLID principle:

SICKLE:

  These three jobs are relatively simple. There are five classes in total. The main class MainClass is responsible for starting and ending other threads and completing the input. The elevator class obtains instructions from the scheduler to run and output, and the scheduler class serves as a buffer. Input instructions in the main class Then, analyze the instruction, mark the instruction for the elevator that needs to be transferred and whether it needs to be transferred, and provide the get method for the elevator class to call. On the basis of the requset class, the people class adds the transfer attribute to describe whether the transfer is required. People saves the attribute after the transfer. The elevator attribute indicates the elevator that the instruction needs to take and the method of setting the from and to floors. The safeoutput class ensures input thread safety. The functions of the five categories are clear.

OCP:

  The elevator thread has not changed much, but the scheduler thread has been modified for three iterations. The rest is not using inheritance.

LSP、ISP、DIP: 

  Except for Thread, all three jobs use inheritance and interfaces.

 

3. Analysis based on metrics:

The fifth assignment:

UML diagram

 

Code lines

Coupling analysis

   

  

Timing diagram

  The fifth operation adopts the producer-consumer model, the overall structure is not complicated, and there are three categories. The elevator uses the LOOK algorithm, the buffer provides put and get methods, and uses synchronized to ensure thread safety. From the perspective of coupling, the realization of elevator LOOK algorithm is very tedious.

 

Sixth homework

UML diagram

Code lines

Coupling analysis

      

Timing diagram

  The sixth job follows the design of the fifth job, which also has three processes. The elevator process is almost unchanged, it is still the LOOK algorithm, and the scheduling between the elevators is randomly assigned. The realization of the electric question LOOK algorithm is still very cumbersome.

 

Seventh homework

UML

Code lines

Coupling analysis

          

Timing diagram

  The seventh operation still uses the producer-consumer model, and a single elevator still operates with the LOOK algorithm. Except for the circulation conditions, the upper limit of the number of people, and the running time, elevators have not made other changes. Floors 1, 3, 5, and 15 are fixed transfer floors. The dispatcher is responsible for dispatching elevators for instructions, while providing Put and get methods. It can be seen that the elevator thread coupling is still high due to no changes to the elevator thread. At the same time, because the request class has been rewritten, the timing diagram is much more complicated.

 

Four. Analyze the bug of your program

  There was no bug in the fifth homework test and mutual test.

  In the sixth homework test, there was no bug, and the mutual test was hacked once. Due to carelessness, when only one elevator is overloaded, the elevator thread may end prematurely.

  There was no bug in the seventh homework test and mutual test.

  In the three operations, the problems when debugging were in the while loop judgment condition of the elevator run method and the elevator wait loop judgment condition. When the code was just written, there was always the situation that the elevator thread failed to end. A lot of time is wasted in these areas.

 

V. Analyze the strategies used to find bugs in other people's programs

  In general, use the special example that comes to mind when testing by yourself + debug the code with the naked eye.

  In the fifth and sixth time, some students used sleep in the room, so they used a command with a long interval (the input time interval between the first and second commands was 50s) to hack successfully.

  The seventh time when looking at the code, I found that a student misunderstood the elevator number of the newly added elevator, and the hack was successful.

 

6. Experience

  The work of this unit feels less difficult. It took a long time to get started with multithreaded programming when I was working for the fifth time. In the other two assignments, the first assignment can be tested with few changes. It took a lot less time than the first unit (although it was because I did not have too high a pursuit of performance). But after three operations, I suddenly found that random allocation could have such good performance. The seventh operation just started a lot of operations to optimize performance, and finally found that it was not as good as the original pure random allocation, and there were more bugs. The risk was finally deleted. Although multi-threaded debugging is really fascinating to me. . But luck was good without troublesome bugs.

  The mental process of the seventh homework was broken. The ctle appeared at the last two points when the test was just submitted. It took a few hours to read the code a few times, and took various means to test it. The results refreshed the website and passed. During the mutual test, I was surprised to find that I was hacked, and the hack disappeared after two hours.

  The first contact with multi-threading, although very difficult, but has a lot of gains. The OO course is over half, I hope it will go smoothly ~ Come on, come on!

  

  

 

Guess you like

Origin www.cnblogs.com/wtrwtr/p/12702634.html