Note: The picture is not clear, right-click the browser to open it can enlarge Orz
The fifth time --multi-threaded elevator
Measure-based Program structure analysis:
Advantages and Disadvantages
Disadvantage is to see the controller thread class or control the overall situation, not a good split to control the local division of labor, the advantage is clear logic.
Design principles:
The explicit expression principle does not do well, many places are used to return 1 state, this habit needs to be improved.
Design strategy analysis and experience:
This is the first time I contact the concept of multi-threading, just start preparing to do when the face is a little more than life can not love, has been suspected can be written out. After this work, the author's greatest experience is that if you do not write this multi-threaded elevator, I am afraid I can not really grasp the object-oriented thinking. One of the process is tortuous, every time feel that they are already close to the best design, but always discuss more problems, so step by step to seek solutions, step by step grinding design ideas, after three days of painful design, Monday began to write code, fortunately, the previous efforts were not wasted, Finally finished writing in Tuesday and started debugging. Tuesday night to see the results displayed correctly when the real happy to explode. For the design idea, I have a lot of ideas to write down.
First of all, the first contact with multi-threading, the most want to ask a question is probably to what object to the thread? What do you want to do when you open a thread and what data structure is used to organize it? What is the interaction object? Where does the thread safety problem occur?
The author suggests that the independence of causality and process should be analyzed first. The process is to simulate the real world, and the real world is the result of causality theory, it is advisable to take elevator dispatching as an example, the main work of the elevator is running, the theme of the scheduler is to take requests, according to the request of the elevator, the logical relationship is the elevator by the control of the scheduler. Because the elevator operation and the dispatcher to the elevator allocation request the two things need to be separate, so for each elevator thread, for the request simulator thread. To continue the analysis,IO processing should be separated from the scheduler, that is, the scheduler is only responsible for seeing if there are any new requests added, while the io waiting for the input request simulator should be another thread (because scheduling is required to do all the Times, While the request simulator may be blocked by waiting for input, this time the scheduler and the request emulator can interact with the shared object by requesting the queue. At this point you will find that after figuring out why these threads are being opened, each thread is divided into modules, and the next thing to do is to analyze the tasks of each module.
Request Emulator Thread: Reads the request, determines the validity of the request, and effectively puts it into the request queue.
Scheduler thread: A host column is required to record all requests to be performed but not assigned to the elevator. Each schedule requires the following work to be done. 1, requests from Request queue Query Request simulator new JOIN request (if not, return empty). 2 , depending on whether the floor lamp is lit to judge all requests for homogeneity, If the homogeneity is thrown out of the home row. 3 According to the main request of each elevator and the current position of the elevator to determine the piggyback request and assigned to the corresponding elevator, if can be assigned to throw out the home row. 4 and the remaining requests from the home team are neither qualitative nor piggyback. means that the queue head needs to find an idle elevator to complete. At this point, if there is an idle elevator, this request to the free elevator and upgrade it to the main request of the elevator, then the next time to make a decision to determine whether the new upgrade master request to be able to piggyback the remaining requests in the home row.
Elevator thread: Requires its own execution request queue (that is, the scheduler is ready for the elevator piggyback queue), every specified time after the end of sleep to query whether the floor to execute the request. After the primary request has been executed, the query for the piggyback queue is empty, not NULL to indicate that there is an er request, then select the earliest generation of er request upgrade to the primary request (the elevator itself to complete the upgrade of the master request) Null indicates that all requests to be executed have been completed and the state is set to idle, indicating that the scheduler is seeking a master request, which requires the scheduler to dispatch a master request.
when these analyses are complete, the security of the shared object interaction is analyzed. Obviously the shared object here is the request queue and the elevator. The request simulator writes to the request queue, and the scheduler reads the request queue into the operation. The elevator thread controls the state of the elevator, and the scheduler writes to the elevator execution queue and the main request. Most importantly, these operations require lock-in and synchronous control. (The first time I write the time no experience no add synchronized lead to read and write different steps de two hours of bug, cry Dizzy)
After completing these analyses, the idea becomes clear and you can begin to coding .
in fact, look back, these ideas are very normal, but then how difficult to think about it Orz. Probably probably because the previous assignments are using the process-oriented approach to direct dispatch, the change of thinking takes time. It is also possible that the framework of the coordination is not clear enough to take the functions of the various parts apart and then perform functional analysis and simultaneous analysis of each object.
to analyze your program's bugs:
This assignment was not found to be a bug in architecture and functionality because of the more rigorous thinking and constant discussion with the small partners . Here is not by the exclamation point, teamwork is really a very enjoyable thing.
to analyze a bug in someone's program :
this time to get the program bug A little bit more, coupled with the author in order to test their own procedures to get a lot of more tricky test set, or found some problems. On the other side of the code, the homogeneity of the judgment there is a problem, when the main request upgrade there is a problem, after the upgrade of the judgment there is a problem, the details of the place with the thread synchronization of the place still need more consideration. The author also read the code of the tested person, and found that there will be no consideration of data boundaries and so on.
Sixth assignment --IFTTT file Management system
Measure-based Program structure analysis:
Advantages and Disadvantages
The disadvantage is that the function inside the monitor controller is too nested, the advantage is the function is clear, the logic is simple and understandable.
Design principles:
The trust principle is not good enough, and the call encapsulation of the function needs to be further improved.
Design strategy analysis and experience:
The idea of this operation is clearly clearer than the last time, coupled with a further understanding of multithreading, in theory is better done. But the truth is, this is one of the most difficult times I have ever felt so far. This assignment for the author only three days, but there are almost a day and a half of the time are reading instructions, discussion areas and groups, after all, the guidance book on the requirements of the explanation is very vague, prone to misunderstanding, such as the document in the end of what circumstances do not need to trace the problem. I think the real code when the IFTTT workload is also very large, and the side of the writing edge will appear more problems, these problems seem to be simply not convergence. After writing the bug(crossed out) code,debug Experience is also the most painful, because I always have a variety of null point using null pointers Crash , and then came to an experience, before using the variable in the code to determine whether the variable is NULL , it is more convenient Bug the location.
to analyze your program's bugs:
this time.Bugare related to the requirements of the Guide book, the public error two, the first is to ignore the guide book about the "thread object up to openTenas the author considers that it is possible for an object to have4trigger, and each trigger will have a3monitoring tasks, so you can open up -threads, they are counted directly to the -. But this is a very problematic, direct counting -Can cause different objects to open -threads, should be counted directly against the object instead of directly counting -; the second one isModifiedThe trigger output to a file has less contentsizechanges in the record, this is because the author assumes that as long as the record triggers change the content of the line, and forget the output of all the file information, the author of the instructions required to understand the output content ambiguity, I hope that the guidance of the future to the content of the output to a clear request. Each one of them was wrong, with the above mentionedNull point crashabout.
to analyze a bug in someone's program :
This time the code is the big guy, write the content is very concise but the function is basically complete, it is worth reading to learn. There is a small drawback is no further tracking of path-changed files, I think this is a guide book a little unclear. There is also a incomplete because there is no way to create a folder, it feels like this is a guide book is not emphasized but the beta gives a bunch of test cases that need to create a new folder. I hope that the guidance book will be more functional requirements to write a little clearer. Find the bug When the author is based on the requirements of the Branch tree to further expand the test.
The seventh time operation -multi-threaded taxi dispatch first time job
Measure-based Program structure analysis:
Advantages and Disadvantages
The disadvantage is that the function nesting of the taxi thread is too complicated because of the transformation of the state machine and the work of transporting the passengers. The advantage is that each kind of division of labor is clear, more balanced.
Design principles:
The explicit expression principle should be followed, and the state of the output should be expressed in string meaning.
Design strategy analysis and experience:
Taxi Scheduling Guidance book is relatively complete, this job scheduling the overall framework is a bit like elevator scheduling. The focus is on the implementation of taxi threads and controller threads.
Controller,FlashcontrollerThe two controller classes are the core of the dispatch, which implements the best taxis assigned to the request at the end of the window, which is designed to provide the appropriate taxi queue for the request during the window period, so the design is intended to achieve"high cohesion, low coupling"the idea that both tasks are scheduled to take time, there is no logical wait relationship, so open two scheduler implementation of the design. Taxithe class uses state machine to realize the transformation of State, and is responsible for running according to the rules after receiving passengers. Between the Threadsinteraction is reflected in the controller according to the status of the request queue and taxi state control and allocation, the interaction of the object mainly occurs between taxi, request queue and controller. Because the request queue is an interactive object that plays"Tray"The class of the role, the method inside the request queue is provided for the request emulator."placed"method, also for the controller to provide"removed"method, where data interactions are involved, soRequestqueuedata structures are thread-safeLinkedblockingqueueTo address the security issues between thread interactions. In theTaxiclass, multiple requests may occur that are assigned to multipleTaxithe situation, whenaskrequest(determine if there are any requests to be executed by the current taxi) method andAddreq(assigning a corresponding request to the corresponding taxi) method may be used by multipleTaxiobject access, in order to ensure thread safety, these methods are addedsynchronizedto ensure thread safety. Similarly,Taxithe other methods of the class also need to be locked to ensure that only one object is written at the same time, avoiding the occurrence of thread concurrencyThere is a metaphysical problem of read-write inconsistency. Requestin the classcandidateis the request to find in the time window can respond to the taxi collection, because add and remove the thread security issues, but also to lock.
to analyze your program's bugs:
this time the program does not have functional bugs, but was deducted the design principle of the "explicit expression principle", the tester that the state of the taxi is not explicitly expressed as a digital expression, the proposed enumeration type. He said the truth, but the nature of the enumeration type is also a digital expression, not to mention the author in the code and comments explain the meaning of the various numbers, and in the process of using the code state is IDLE, todst,WFS,tosrc Express, but the output to the document when the direct use of numbers, I think this is a bit of a serious.
Class state{
static final int IDLE = 0; Stop State
static final int todst = 1; sending passengers to their destination status
static final int WFS = 2; Waiting for service status
static final int tosrc = 3; to pick up the passenger's status.
}
to analyze a bug in someone's program :
The procedure was interesting, and the tester opened a thread for each request. I think he might want to take a tight control of the time window at 3s, but in fact there is a big risk that his purpose is not necessarily achievable because the JVM 's thread scheduling time is not something we can control, And this will cause multiple requests to access the taxi state at the same time because of the excessive number of requests resulting in longer wait time, and most importantly, for the request to open so many threads, and there is no limit to the number of requests, my eclipse directly to his run down Orz. I entered the instruction at one time, directly reported the heap full and memory full of crash, and then Eclipse is basically stuck in a dead state. There is a problem with the output of the test, not directly take the snapshot output at the time, resulting in the judgment looks problematic, and the passenger location is also problematic. Another is that the beta does not determine whether the request starts and ends are the same.
Object-oriented course multithreading summary