My very imperfect programming experience (2)

Another month has passed, and the second unit of the OO course has come to an end.

Thinking back to the course goal of this second unit, it's clear that one key point can be grasped: multithreading.

So here comes the first question.

Q1: What is a thread? What is the use of multithreading?

A1: This issue is also explained in detail in the operating system course. In layman's terms, a thread can be thought of as an "alternate" assigned by the main program (process) to perform a task. As for the value of multithreading, I don't have a deep understanding of it myself, but from a practical application point of view, it can support multiple tasks that need to be parallelized. And multithreading can effectively reduce the program execution time.

Q2: I still don't understand, what is the inconsistency between multi-threaded Java and single-threaded C?

A2: According to my own understanding, single-threaded C is dominated by the main function main. The entire program requires the main function to perform all operations in the process, and "all in one" function calls and data maintenance. In layman's terms, the main function is very "hardworking", but it will inevitably appear to be alone. But in Java multi-threaded programs, the main method is only used as the "starter" of each thread. The main method itself is "lazy". It assigns its tasks to each thread, "stocks" them and lets them do their own work. of. This is equivalent to having a mode of cooperation under a supervisor.

Then, let's understand the mystery of multi-threading together in practice.

 

The fifth assignment - multi-thread elevator

Unfortunately, due to the failure of the implementation of the elevator piggybacking algorithm in the fourth operation, the fifth operation was not completed directly. (Well, this is the pot of my personal ability, but even if the deadline for homework has passed, I still have to make up for what I haven't learned, and I can't let this hole exist forever.)

However, I think the difficulty of the job is not actually the operation of the multi-threaded specific code, but the focus is on assigning the task of the elevator based on the amount of movement. From the perspective of real life, this is also more reasonable, which can save the energy consumption caused by the no-load operation of the elevator.

Q3: I seem to think something is wrong... What if two elevators grab an order? When the scheduler gets the elevator state, what if one elevator secretly changes the state?

A3: Well, this question...I haven't thought about it...Can I talk about it later?

 

Sixth assignment - IFTTT file monitor

When I first got the task of monitor, my first reaction was to make an "instant" monitor. As long as the file changes accordingly, it should respond immediately. But after thinking about it carefully, I found that it couldn't be done at all. The main problem is that changes to the file cannot be notified to the monitor, and the monitor must be actively probed. The acquisition and judgment of the file status by the monitor can be considered as an instantaneous process.

Therefore, the monitor must scan the file directory at a fixed frequency, and at the same time sense the file status, and achieve the goal of real-time update by multiple refreshes at short intervals.

With this purpose in mind, there is a clearer guideline for multithreaded design. Every time a file is received, a new thread is created, the corresponding task is selected for monitoring, the file information is compared, and the corresponding operation is performed if the corresponding condition is triggered.

Essentially, this assignment is not too demanding in terms of design. The corresponding scanning file directory, obtaining file information, setting trigger conditions, executing operations, etc. can all be encapsulated in a method, and there is no mutual interference between methods.

The difficulty should be the association between Java file classes and real files, and: how to prevent threads from interfering with each other, that is, thread safety.

I have a vague impression of thread safety, adding a lock that only allows one thread to modify the file at a time. However, access to file status, ie "read" operations, is unlimited. Because the read operation does not change the file information, that is to say, the result of the read operation in any order is the same.

Q4: So I have a clue about my question just now?

A4: A little bit. It is to add a lock before each method, limit the operations to be performed, and finally release the lock, which is equivalent to forcing each thread to perform a "queuing" operation.

Here is some analysis of my code:

(Class Diagram)

(diagram)

It's a pity that my homework was staggered and put into the git repository for answering questions, which made the homework invalid...

But when I tested it myself, I found a lot of things to improve.

1. During the test, the limit of the number of threads was changed to 1, but it was not changed back.

2. The operation of outputting to the file is not detailed enough, and it is not encapsulated.

3. No test interface is provided.

4. During the recovery operation, I found that I could not put the file back to its original location, but could only modify the information such as the last modification time, file size and file name.

 

The seventh operation - taxi-hailing system

This homework provides a GUI interface for observation for the first time. (It becomes a lot more interesting) However, the core question remains: how to avoid the rush between taxis?

Needless to say about the multi-threaded design. My design is to have the 100 taxis all run as separate threads and the scheduler as a separate thread. After inputting the request, the InputHandler class will be called to interpret and judge the validity. If legal, add to the instruction queue. The scheduler scans the queue every 100ms and collects available taxi information if the time does not reach the window closing time. If the time is up and there is a car, the order will be deleted, and the taxi with the highest credit and the nearest taxi will be selected according to the requirements in the guide. The taxi received the instruction, changed to pick-up state, took the shortest route to the passenger's place, and then sent the passenger to the location. If there is no car, it will prompt no car. Other than that, taxis are moving freely.

(Class Diagram)

(analyze)

It can be found that in the analysis of the Taxi class, the run method calls an extremely verbose method. One is the wander() method, which is used to implement the random movement of the taxi. But each execution requires the taxi to calculate the connectivity of the surrounding four directions, which is very slow. In addition, there is an algorithm to the target point in other methods, and the code reuse is not done well.

Mutual testing:

Test my classmates did not find my bug.

I made an oolong while testing. My program can directly output error information in the console, but the classmate's output is directly in the file. I take it for granted and think this is a bug. After the classmate appealed, I immediately corrected the mistake and wanted to say sorry to that classmate.

In addition, this assignment emphasizes the principled nature of the design. I don't have a corresponding in-depth understanding of this, but have some vague understanding of this.

 

I still hope that we can work together to improve our programming and design level, don't give up, come on.

Guess you like

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