Object Oriented Course Second Essay

Coordination and Synchronization Control of Multithreading

fifth assignment

  As the first multi-threaded job, I don't know much about the synchronization and coordination mechanism of multi-threading. In this assignment, I have three elevators as three threads, the input thread and the scheduler thread to assist. A very simple producer-consumer pattern is adopted between the input thread and the scheduler thread to pass instructions. What is more important is the coordination between the scheduler and the three elevators, and the shared resource is the state of the command and the elevator. Since it is necessary to consider the state of the elevator to assign instructions, the synchronization of the elevator state is very important. I designed a special method in the elevator class to return the elevator state and synchronize this method to ensure the elevator state obtained by the scheduler and the actual elevator state. synchronization.

sixth assignment

  The shared resources of this job are mainly the files in the directory. The detection of the files in the directory and the operation of the files in the directory should be carried out at the same time. A very important synchronization measure is to design a thread-safe file class to ensure synchronization when the file is changed. I personally had some issues with this design. The first is the establishment of threads. For each monitoring instruction, I create a monitoring thread, and then this monitoring thread continuously scans the files in the monitoring directory to obtain snapshots. The advantage is that it seems that there are fewer threads that are opened without any problems, but there are too many problems in actual processing. The most critical point is that the synchronization of file operations cannot be guaranteed. Even if I lock the operation of the file, due to the same file (with the same path), the file index established in the two threads is different, and thread safety cannot be guaranteed at all.

The seventh homework

  This assignment is about a car-hailing problem for 100 taxis. I personally think that the thread-safe design this time is not fundamentally different from the three elevators. The difference lies in the scheduling strategy and more reasonable code design. The key is still to control the shared resource of the taxi state, and it is necessary to ensure that the state of the taxi is locked. For each request, I don't like to open a thread for each one, which leads to too many threads. My approach is to put them in the scheduler thread, scan them continuously, and automatically clear them after 3s.

Analysis of programs based on metrics

fifth assignment

 

   The above is the code analysis of this assignment. It can be seen that the McCabe cyclomatic complexity is still red, and the variance itself is not too small, but compared with the previous assignment, it has been greatly improved. The improvement mainly comes from the separation of input control, scheduling and elevator classes. The maximum depth is okay. The maximum depth is in the run method of the scheduler class, which has 6 layers.

sixth assignment

  The overall complexity of this job is similar to the last time, and the iteration depth is slightly deeper, probably because I used a recursive way to check the files in the directory.

  This is the class diagram of this assignment. It can be seen that this design is relatively mindless and has not been carefully planned. Just simply create a watch thread for each directory.

The seventh homework

  Compared with the previous two assignments, both the number of lines of code, the number of classes, and the number of methods have increased. It's worth noting that the variance of the cyclomatic complexity is a bit too large

 

  The reason is that the findNextPlace method in the Taxi class is worth noting, this method is used to help the taxi find the next place to drive. Due to the doping of many cases, even including BFS, the complexity is very large. Consider rewriting this method for the next assignment, splitting its functionality and reducing complexity.

  As can be seen from the class diagram, this job is similar to the fifth one, 100 taxi threads are running at the same time, and the shceduler is responsible for their allocation

Analyze your own bugs

fifth assignment

bug1: In the case of INVALID, the brackets are also removed, and the original output should be the content of the input.

bug2: Instructions in a line are separated by ;. For the input given, there is no instruction between ";;" and an error should be reported. I directly regard it as no instruction and no error is reported.

sixth assignment

bug1: identical requests were not ignored

bug2: I didn't consider what to do when there were more than 100 instructions. When there were more than 100 instructions, I directly chose to ignore the following instructions. However, the instructions within 100 may be invalid, so there will be bugs when too many instructions are entered at one time.

bug3: There was an error in logging the file recursively, resulting in less logging of the file than it actually was.

bug4: I did not understand the definition of modified in the guide book accurately. I mistakenly thought that modified is only the last modification time.

The seventh homework

No bug found yet

Strategies for spotting bugs in others

  The inspection is mainly for the inspection of the nodes on the test tree. The steps are:

  Basic function check---view code---analyze shared resources---construct test cases for shared resources, check thread safety---stress test

  As a program running for multiple threads at the same time, it is more prone to bugs in stress testing and inspection of shared resource management. The reason is that many threads are opened during stress testing, and shared resources lead to unsafe threads.

own analysis, summary and reflection

experience

Design Principles

(1) Ensure the balance of classes and methods.

  There are things like god classes that are prone to bugs.

(2) Centralized data management.

  Many times I will write a group as a separate class for easy management, such as the state of an elevator.

(3) Implement methods in the underlying class, provide interfaces, and call the top-level class directly to make your code more layered,

thread safety

  My analysis idea is to not consider how to ensure thread safety at first. Design classes and methods first, and then consider the interactions between them to find shared resources. Finally, analyze how to perform some locking and synchronization measures to ensure thread safety. However, some thread safety designs are not very good, and simply using synchronized sometimes reduces the efficiency of concurrency. In the future, you should try some of the better thread-safe designs seen in books.

 

Guess you like

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