OO second essay

1. The fifth assignment

1. Class Diagram

  Because this time the elevator needs to simulate real time operation, and I used to judge the homogeneous piggyback by time operation, so the previous elevator scheduling can not be used, this time it is basically a complete rewrite of an elevator. From the class diagram, the distribution of each class is relatively balanced, but my status is represented by numbers, which does not conform to the display principle, and the design style of the code is not particularly good.

2. Metrics

 

From a metric point of view, my cyclomatic complexity and nesting depth are very popular. It seems that I have to study how to not nest so deeply. .

3.sequence diagram

4. Analysis

(1) In the design of the thread

  A total of five threads of ReqSimulator, ElvRun (*3), and MultiScheduler are set up in this job. ReqSimulator and MultiScheduler share one tray RequestMonitor, and ElvRun and MultiScheduler share three elevators. ReqSimulator is responsible for receiving incoming requests, placing a line of requests into the shared tray RequestMonitor, and MultiScheduler stores all pending requests (sharedReqQueue), the scheduler is constantly responsible for scanning the request queue, and if there is a request that can be executed, the request will be Take the elevator assigned to the corresponding elevator from the queue. Before each scan, the MultiScheduler tries to fetch the input request from the RequestMonitor, and if there is one, it is fetched once and put into the request queue. On the issue of thread synchronization, because RequestMonitor cannot put and take at the same time, it is necessary to add a lock to his method. At the same time, MultiScheduler and Elevator share a light (the floor class and the light in the Elevator class), so the light can not be turned on and off at the same time, so the relevant methods in the Floor class and the Elevator class should also be locked.

   Because MultiScheduler needs to access the properties of Elevator, I am afraid that there will be problems if Elevator is a thread, so instead of using Elevator as a thread, an EleRun thread is written to manage the movement of the elevator. MultiScheduler and EleRun share Elevator. ElvRun controls the direction of elevator operation by reading the status of the elevator, and does not change the status of the elevator, while MultiScheduler also reads the status of the elevator, and assigns the main request to the elevator or replaces the main request or assigns the piggyback request according to the elevator status.

(2) Bug analysis

  One of the problems in this work is that there is no snapshot of the elevator status in MultiScheduler, and the allocation of requests is judged according to the snapshot. In this case, there may be errors in the program operation. There was a bug in the public beta test this time, that is, there were some small problems when selecting multiple lifts that could be piggybacked according to the cumulative amount of exercise, which led to a public beta test.

  The jobs I assigned will only be output in the file with illegal input, but the legal input does not seem to respond, and there is no output in the file, so the public beta cannot pass except for the illegal input sample.

Second, the sixth work

1. Class Diagram

   

  From the class diagram, four triggers are placed in the Monitor class this time. The Monitor class selects which trigger to execute by judging the trigger type, and there may be several tasks in a Monitor, so the Monitor judges that the trigger condition is established. It is necessary to perform three if judgments to complete the corresponding tasks. In this case, the code in the Monitor is complicated and redundant, which does not conform to the design principle of single responsibility.

2. Metrics

 

 

My cyclomatic complexity and nesting depth are still very red, probably a large part of the reason is that the design of my Monitor is too redundant.

3.sequence diagram

 

4. Analysis

(1) In the design of the thread

  This job opens threads according to triggers, and a thread is uniquely determined by a monitoring target and a trigger type. In the monitoring thread, there is a snapshot of the previous state of the monitoring range, and a snapshot of the monitoring range at this time is taken after each cycle of sleep is completed. Scope old snapshots and snapshots of monitoring targets. Because the File class provided by java is not thread-safe, I encapsulate the File class into a SafeFile class (though the goose encapsulation is not very good). At the same time, the attributes of the SafeFile class store a snapshot of the monitoring target, and each time the judgment ends, the update method in the SafeFile class is called to update those attributes. Because the Monitor class does not need to access the properties of the SafeFile class at any time, but only needs to take a snapshot before starting the judgment, so there are not so many issues about thread safety, just lock it in the SafeFile class. But when it comes to organizing snapshots, because I don't know how to use trees, and I don't understand some useful tools provided by java, I can only use arrayList to store snapshots, which makes my program not very efficient.

(2) Bug analysis

  Before I started to write this homework, I thought that this homework would be easier to write than the last time, but I found out that I was too naive. The last homework had a Qingming holiday so I could study how to write it. I was still in the middle of this homework on Monday night. The state of being unable to do anything and not finishing it, led me to despair and tried to give up. Fortunately, I pulled back my feet and finished writing it. But because the testing time is not enough, I still have some bugs, because I haven't tested much when the monitoring target is a directory, so I have not found a big bug related to the directory, which directly affects most of my monitoring target is a directory Function, there are a lot of bugs.

  The code I got in this assignment is very clearly designed, and I didn't find any bugs.

3. The seventh assignment

1. Class Diagram

 

   In this assignment, special attention was paid to the planning of classes. The responsibilities of each class were clearly planned. A state machine was placed in Taxi to control its movement. The Scheduler was responsible for scanning the request queue, dispatching and grabbing orders for requests. After completing the initialization, it is responsible for reading the request and adding the legal request to the Scheduler. In principle, four constants (private static final int) are used in the state of the taxi, and no enum is used, so it does not conform to the display principle, because the taxi controls the movement through the state machine, and the run method may be redundant, but I can't think of a better way to control its movement.

2. Metrics

 

 

 

3.sequence diagram

 

 

4. Analysis

(1) In thread design

  This job has a total of 102 threads of Taxi (*100), Scheduler, and TaxiSys. TaxiSys and Scheduler share a RequestQueue. TaxiSys receives the input request and puts it into the request queue. Scheduler accesses the request first if the request queue is not empty each time it loops. The head of the queue, if the order grab window of the head of the request queue should be closed, the request will be taken out of the queue and the order will be dispatched for him. Next time, the head of the queue will be judged until the queue is empty or there are no requests that should be dispatched, and then the request queue will be sent to the head of the queue. All requests in rush orders. In terms of thread safety, TaxiSys and Scheduler share a request queue, so a lock must be added to the request queue, and the restriction cannot be put or taken at the same time. Every time the Scheduler needs to use the status of the taxi to judge, it needs to find the snapshot of the taxi and use the snapshot to judge, instead of directly accessing the status of the taxi to avoid errors.

(2) Bug analysis

   There is a small bug in this program, that is, when obtaining the shortest distance from the taxi to the passenger when requesting dispatching a ticket, if the coordinates of the two points are the same, the length of the shortest path obtained is not 0, and finally find my dijkstra function to return There is one place where the array subscript is wrong in the shortest path length (really makes me blush).

  No bug was found in the code of the classmates obtained in this assignment.

4. Summary

 (1) The first contact with multi-threading is really very painful. During the test, I always found some bugs that made me blush (I still feel that it is the pot of multi-threading). At this time, I think it is necessary to first crazy output to judge whether there is a problem in addition to the judgment condition or a problem with thread synchronization.

(2) The strategy for finding bugs is basically to search according to the bug branch tree, especially the harder it is to test when you go to the back. You can only test according to the branch tree combined with the test thread to check the status.

(3) When I first came into contact with multithreading, for multithreading synchronization, I would only add synchronized in front of the method. Although this may affect the execution efficiency of the program, because of my limited ability, I have not tried lock and other mechanisms, and now I will only use synchronized.

(4) I feel that I have obviously more things to think about before writing code. I have to read the instruction book first and then think about how to plan and what thread safety issues are involved. I will also discuss it with my classmates and roommates, and only after thinking about it clearly. will start writing. When writing, it also abstracts reusable things into a function to reduce code redundancy. However, there are still problems with design styles and design principles, and they still need to be improved continuously.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325115717&siteId=291194637