OO second stage documentary


$ 0 is written in front

It is often those particularly painful experiences that bring people the fastest growth. In a blink of an eye, half a semester has passed, and the time is in a hurry. No matter what kind of fear I was full of these times before, with the help and efforts of my friends, I gradually got through the difficulties one by one. Looking back on the road I have traveled, I am full of gains and a sense of accomplishment.


 

 

$ 0-0 multithreading or multiprocessing

In OS, another core professional course of this semester, we came into contact with the concept of process and thread, and we had a preliminary perceptual understanding of the operating mechanism of the two at the operating system level. In recent assignments, we have really applied the multi-threaded programming idea to the development of the engineering projects we have written, and also took this as an opportunity to experience the various characteristics of multi-threaded programming, which also triggered a lot of thinking and accumulation. a lot of experience.

$ 0-1 Multithreading Uncertainty

The essence of multi-threading is concurrency, and multi-threads occupy CPU computing resources in turn, so each statement in a formally continuous code block may be interrupted. This behavior of interrupting and switching to executing other thread statements introduces uncertainty in the running results of multi-threaded programs, and we often expect a deterministic running result for the same set of inputs. This uncertainty is What we don't want to see.

$0-2 thread safety

From the discussion triggered by the above paragraphs, we can see that the uncertainty of multi-threaded programs is what we need to try to avoid by relying on code logic in the programming process. If it is interrupted only in the data access stage, only the data calling process will be affected; if the data writing stage or the logical judgment stage is interrupted, it may have very serious consequences, including running result errors, entering Infinite loop and even program crash and so on. Therefore, it is necessary to take all possible synchronization mechanisms and lock control to ensure thread safety.

$0-3 Importance of Design

Design is the eternal theme of engineers. In a recent class, the teacher introduced the design principles that need to be followed in the engineering development process in simple language. The design process is an essential core step in a project. A reasonable design can greatly optimize the coding, reduce the difficulty of testing and debugging, and improve the readability of the code. On the contrary, if the design work is not sufficient and the "hand precedes the brain action" eager to code, it may be twice the result with half the effort, and the more serious consequence is that it will eventually need to be pushed back as a whole. Therefore, in the following description, I will record my experience at the design level.


$1 Multithreaded elevator


 

$ 1-0 A first look at "multithreading"

Multithreading refers to the technology that realizes the concurrent execution of multiple threads from software or hardware. Computers with multi-threading capabilities have hardware support to be able to execute more than one thread at a time, thereby increasing overall processing performance. Systems with this capability include symmetric multiprocessors, multicore processors, and chip-level multiprocessing or simultaneous multithreading processors. In a program, these independently running program segments are called "threads", and the concept of programming using it is called "multithreading". Computers with multi-threading capabilities have hardware support to be able to execute more than one thread at a time, thereby increasing overall processing performance.

$1-1 Application of the classic model of multithreading

In the implementation process of the multi-threaded elevator, I adopted the classic multi-threaded "producer-consumer" model, the input terminal InputHandler is the producer, the scheduler Dispatcher is the consumer, and the public object request queue is the "tray" in the model. . The producer and the consumer are independent threads, and take turns to obtain the right to use the public object lock. The synchronized keyword is used to ensure that only one thread can access the shared object at the same time.

$1-2 Defense and Offense

In this round of mutual testing, because my understanding of the producer-consumer model was not deep enough, there was an unreasonable wait when the two took turns accessing the common objects. The main reason for this bug is that the design level is not well thought out, and the scheduling method of the scheduler is seriously unreasonable. Since the scheduler scans requests from the request queue and allocates them after the input is completed, the requests waiting in the request queue may not be responded in time. The specific response timing depends on the time when the input ends.

For the repair strategy of this bug, I adopted the method of canceling the waiting, so that the input terminal and the scheduling terminal scan the request queue concurrently. This avoids the request in the fetch request queue from relying on the input end flag.

$ 1-3 Code Metrics Analysis && Class Diagrams

                                                     

From the above analysis results, the main reason for the excessively large cyclomatic complexity and nested block depth is the method of checking the motion state of elevators. In order to better optimize the code quality of this part, the abstraction level of the code can be further improved, and similar parts in different inspection methods can be merged.

【Class Diagram】


$ 2 IFTTT


 

$2-0 thread safe instance

In this assignment, I have a better understanding of thread safety.

Thread safety means that when multi-threaded access is used, a locking mechanism is adopted. When a thread accesses a certain data of this class, it is protected, and other threads cannot access it until the thread finishes reading, and other threads can use it. There will be no data inconsistency or data pollution. For a class to be thread-safe, it must still behave correctly as described above when accessed by multiple threads, regardless of the timing or interleaving of the execution of those threads by the runtime environment, and not in the calling code. any additional synchronization. The effect is that operations on thread-safe objects occur in a fixed, globally consistent order from the perspective of all threads.

Since this assignment highlights operations on files, the java.io.File class is frequently used. But the File class is thread-unsafe. Therefore, based on the design consideration of thread safety, I established the SafeFile class to lock the various methods provided in the File class to ensure the atomicity of each method during the execution process and avoid the occurrence of uncertainty during the operation process.

$2-1 Simplify complex problems

Due to the complicated content of the guide book and high requirements for understanding the design angle, it is necessary to reasonably abstract the specific design requirements, and classify the requirements into the following points. This process is to simplify the complex problems in the design work.

1. For the case where the target path is a directory

(1) According to the requirements of the instruction book, even if the target path is a directory, the actual monitoring is only the files in the directory, and the directory is not within the scope of monitoring

(2) If the files in the directory are changed, the processing method is the same as the processing method of the target path as a file, and the situation where the target path is a directory can be understood as the target is a set of files

2. For the case where the target file disappears and appears

(1) According to the requirements of the instruction book, the four triggers are only triggered when the target file exists, so the appearance of a file will not trigger any trigger.

(2) For the disappearance of files (renamed or moved files cannot be found), it is not within the scope of the triggers described in the guide, and there is no need to trigger

3. For the case of multiple triggers on a target file, assuming that a file has all four triggers,

(1) Each trigger is independent of each other (corresponding to the statement that each trigger has one thread in the instruction book)

(2) Modification of file content (writing new information, not deleting) will trigger size-changed and modified

(3) If the file is renamed, renamed will be triggered, and the other three triggers will be invalid (path-changed requires that the file name cannot be modified)

(4) If the file is moved, path-changed will be triggered, and the other three will be invalid, (renamed requires the file name to be changed and the file is still in the original directory)

(5) The failure mentioned above refers to not continuing to track

(quoted from gitlab Homework6 issue #32)

$2-2 Defense and Offense

During this round of mutual testing, my program was found to have failed the directory monitoring of path-changed, causing all the test points of this branch to fail. The reason is that in the process of writing the path-changed trigger thread, the monitoring of the directory needs to traverse all the files in the scanned directory in each clock cycle, and because my scanning method is named improperly, it is recursive scanning. The wrong method name is used in the process, which in turn invalidates the monitoring. The thinking caused by this lies in the explicit representation principle of the design. If you even have a name that may be confusing, it will definitely reduce the readability of the code to a large extent.

$ 2-3 Code Metrics Analysis && Class Diagrams

 

    

 

In the results of this engineering metric analysis, we can see that a new indicator exceeds the standard value - the number of parameters. The reason this metric was out of range was that I was passing too many parameters (up to 8) to a particular method or constructor. The disadvantage of too many parameters is that when parameters are passed, the relative position of each parameter is prone to errors, resulting in the failure of the method.

The solution can adopt the following strategies, reasonably classify according to the characteristics of the passed parameters, and establish a designated class to manage the data to be passed. Raise the level of abstraction in design.

【Class Diagram】


$3 taxi dispatch system


 

$3-0 System Architecture and Design

Since taxis and elevators belong to a series of operations, the overall design should be an important factor to be considered. In this assignment, 12 important design principles were deliberately considered. However, due to my limited ability and the fact that I was designing with many design principles for the first time, I could only try my best to understand the ideas in it, and I could not guarantee that it completely conforms to the design principles. The 12 design principles are required to be fulfilled.

The design principles mentioned in the requirements are as follows:

1) Single Responsibility Principle

2) Open Close Principle

3) Liskov Substitution Principle

4) Interface Segregation Principle

5) Dependency Inversion Principle

6) Hierarchical abstraction principle to identify classes according to the logical relationship of the problem domain;

7) The principle of balanced distribution of responsibilities to avoid the appearance of God and Idiot classes;

8) The principle of localization, do not store the same data redundantly between classes, and control coupling cannot occur between methods;

9) Reuse principle (common extraction principle), abstract common data or processing between different classes into inheritance relationship to avoid redundancy;

10) Explicit expression principle, express all the data or logic you want to express explicitly, and do not use array storage locations or constants to implicitly express a specific state or data;

11) The principle of trust, when a method is called, the caller needs to check and ensure that the basic requirements of the method can be met, and after obtaining the result of the call, it needs to be handled according to the agreed upon various situations;

12) Know my principle, all classes, objects, variables, methods, etc. are named "as the name suggests".

(Excerpt from [The 7th Homework Instructions])

In addition, due to the characteristics of the series of jobs, I have to reserve relevant interfaces for the subsequent expansion requirements, and I should also spend more thought on the design of the program's scalability .

$ 3-1 Algorithm optimization

In this engineering task, due to the consideration of the shortest path, it is inevitable to design the BFS search algorithm for the single-source shortest path and graph. Some of the BFS methods are provided for design-time invocation in the GUI provided by the job file. However, due to the limitations and high complexity of the algorithm, this algorithm cannot be directly applied in the project. Therefore, I optimized the provided BFS algorithm, and uniformly initialized the storage of the connected graph matrix, which greatly improved the performance of the program.

$3-2 Defense and Offense

During this round of mutual testing, I found a total of 1 bug. This bug was caused by my misunderstanding of the instruction book and issue. This reminds me that when reading a certain design requirement, do n't take it for granted . You must communicate and verify with your classmates, and on the premise of ensuring correct understanding, focus on design and start coding.

Also, a design flaw was reported due to a small amount of coding behavior in my code that uses numbers to record state information, which I have fixed to further improve the readability of the code.

$ 3-3 Code Metrics Analysis && Class Diagrams

      

From the metric analysis results, the first taxi project code implementation is ideal, and most of the indicators exceeding the standard are the problems of the GUI provided and the methods in the Main class.

My own coding problem is mainly reflected in the implementation of the shortest path algorithm for taxi movement. The two processes of taxi pick-up and delivery use the exact same algorithm, but the point is different. Therefore, for me, I constructed two It is not good to have an almost identical method, so I modified this part of the code and constructed a method with a higher degree of abstraction to implement related functions. In addition, the check method of the input class implements too many functions. It aggregates functions such as regular matching, separation and extraction of information, which introduces excessive complexity, which I will pay attention to in the follow-up project.

【Class Diagram】

 


$ 4 is written after


 

This experience of multi-threaded programming is unforgettable for me, and relevant knowledge is also indispensable in future work. Since this is the first time I have come into contact with the concepts of multithreading, and the time is very limited and urgent, there are still some questions that trouble me. I believe I can have a more in-depth experience and understanding in the next few code assignments. At the same time, because its knowledge content is highly abstract and difficult, it is not enough to rely solely on individual efforts. This course gave me a good inspiration, that is, cooperation and communication. Whenever I encounter doubts, after my own exploration, there are still some details that are not clear enough. At this time, I will exchange and discuss with my classmates, and there will always be unexpected gains. In the design process, after a period of independent thinking, I also like to "brainstorm" with my roommates. Everyone can speak freely, share their ideas, and learn from each other. I can always learn from them to optimize my design ideas.

I think several assignments have brought me not only the improvement of engineering design ability, but also the experience of exchanging technology with my classmates and training my communication ability, which will provide a lot of help for my future work.

Guess you like

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