Oo Object-oriented multithreaded programming job summary

Source: Internet
Author: User

Tag: The schema causes the Observer mode AMP run that also requires a parenthesis thread test

Fifth assignment: Multi-thread elevator scheduling design Strategy

? In this elevator operation, I constructed an elevator request queue thread, a scheduler thread, three elevator threads, a file output thread, and a main thread.

? The scheduler scans the user's request queue, assigns each queue to the elevator that meets the requirements, each elevator has its own request queue, and the elevator changes its state according to its own request queue.

? Synchronization control mainly consists of two points: the user request queue will be the dispatcher and the request queue input thread operation, need to synchronize control, the elevator inside the running queue will be the elevator itself and the scheduler operation, but also need to synchronize control.

? On the synchronization control, I used the Synchronized keyword and wait () and notify () methods to implement thread-safe operations on shared resources.

Metric Analysis and Class diagram
    • Class Diagram:

    • Metric Analysis:

In the measurement analysis of the class, the complexity of the scheduler class and elevator class is higher, then the complexity of the method is analyzed.

In the scheduler, it is possible to determine whether the method can be carried in a high degree of complexity, in fact, this part can be done by simulating the elevator button, mentioned in the bug analysis. Dispatch Elevator method Complexity is also high, mainly to judge the elevator outside the request, analysis of each elevator cumulative amount of exercise, etc., here can be more modular.

Elevator class complexity is mainly the method is too many, each stop on a floor after the shortest request to be recalculated. Changing ideas can make your code a lot simpler.

and my request. In the queue of the run () method, I put all the input of the various judgments written in the run, it is very bad habit, should let the thread run method as concise as possible, the input operation module.

Bug Analysis

I found a very important bug in this assignment a few hours before I submitted it, which was a flaw in my design thinking, and the instruction book was not read carefully. In the request to join the request queue, if the elevator in the request should be directly assigned to the corresponding elevator, do not have to wait for the elevator to be able to piggyback or wait for service status, and I let the elevator inside the request waiting. In the last modification, the request was forcibly added to the elevator queue, which led to a piggyback problem.

In fact, better design ideas should be simulated real elevator, to the elevator each layer set button, when there is a request to send, change the state of the button, in each layer run at the time to determine whether the button is pressed, if the same quality request is ignored.

There is a greater error in the control of the thread time, not particularly accurate.

Find someone else's bug

This time I test the classmate's bug is mainly in the format problem above, the output when the parentheses are not in accordance with the requirements of the guidance book output.

Sixth assignment: IFTTT file management system design strategy

In this job, I open the input controller from the main thread, get the file monitor that needs to be opened from the input, the monitor thread starts to monitor the file in real time, the test thread also opens, modifies the file, the monitor detects the change of the file, sends the information to the operation thread, and performs the corresponding action on the corresponding file.

In the aspect of synchronization control, it is necessary to synchronize the reading and modification of the file information.

There is also a stop thread to control the system termination, enter the appropriate instructions, the system will automatically exit.

Metric Analysis and Class diagram
    • Class Diagram:

    • Metric Analysis:

In the Inputhandler, the average operating complexity is higher, because the code to judge the format is longer, you can consider more modular judgment.

The average operational complexity of each trigger is also high. This is also related to my design ideas, because at first did not notice that many triggers need to monitor all the files in the directory changes, so initially did not monitor the folder, and later increased the folder monitoring, the code becomes verbose.

In fact, you can use nested loops to monitor the individual files to implement the folder monitoring, so that it will be more concise to write.

Bug Analysis & Self-analysis
    • Bug

When testing a deeper directory, my output will be very unstable, such as not deleting the previous recorddetail.txt is easily unstable, thread-safe issues are not resolved.

    • Self-analysis

I put all the classes in the default package, so the architecture is not very good. Both the Monitor class and the action class can be placed in separate packages, and the Monitor class can also be written as a way to inherit a parent class, and they all have the ability to monitor the file. My program has a lack of extensibility and maintainability.

The good part of the program is the use of a operation class to choose the operation, each time only need to call operation, and do not need to invoke the method of each operation separately.

Find someone else's bug

The other side of the design of the security of the thread is missing, many files have been modified after the monitor can detect, but did not do the corresponding operation, resulting in a lot of output is empty.

Seventh assignment: Design strategy of taxi dispatching system

For every taxi, I open a thread, every request that meets the requirements, I will open a scheduler thread to dispatch the taxi, the requirements of the taxi to join the scheduler thread queue, the scheduler thread after running 3s, Select the most appropriate taxi from the queue and send the request to the taxi to execute, during the request execution, the taxi needs to be locked, preventing other scheduler threads from dispatching the taxi.

Metric Analysis and Class diagrams:

Class Diagram:

This time the different modules are placed in different package, information used to store data, taxi used to store taxis and taxi queue, Locaiton used to store coordinates and maps, scheduler used to dispatch, passenger to store the request queue , enter the request.

Metric Analysis:

In addition to the GUI, the average complexity is the taxi class, and Taxi's move and state transitions do require many functions to determine the implementation, so the inevitable complexity increases.

Bug Analysis & Self-analysis
    • At the end of the line, I did not judge the mistake, but let the taxi take orders. There is no real-time output of the CAB information file output.

The old problem, the output time interval has the error. found that the system's sleep method does produce errors, so the latter wrote a sleep method for precise control of sleep time.

    • Originally this job want to use the observer pattern, but because of the observer class is not many, I used the Observer and notifier after the code becomes very jumbled, so finally still give up the use of this method, feel design mode also need more research.
Find someone else's bug

The time of the thread scheduling found the problem, and input two of the same request was treated as a two-time request, possibly a delay in the intermediate code.

After the taxi free Walk, through the console output, found that some did not stop at the specified time for one second.

There is no output for the process of finding the shortest path.

Experience

1, in the code structure and design patterns also need more research, from the beginning of all classes are written together, to the face of different classes of functional categories, there are some progress. The use of interfaces and abstract classes requires additional practice to enhance the maintainability and extensibility of the program.

2, the control of the thread, always in the time of the accuracy of some problems, mainly in the scheduling of the thread will have some delay, these problems have not found a suitable solution, now can do is to try to simplify the code to run the volume.

3, feel every time do homework before the system to start writing code, resulting in a lot of links will not be written again to go back to learn, and there is no overall framework. It is still essential to consolidate the basic knowledge.

Oo Object-oriented multithreaded programming job summary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.