OO second course summary

  The three jobs are multi-threaded elevators, file monitors, and taxis. These three jobs are not the same topic, but these three jobs are multi-threaded problems, which has been greatly improved compared to the previous single-threaded difficulty.

fifth assignment


 

  The fifth assignment is the last assignment for the elevator topic and the first assignment for the multithreading problem. This assignment is even more difficult for me, because the first two assignments of my elevator were process-oriented code, so this time, if I want to implement a multi-threaded elevator, the only way is to rewrite it. And after changing to process-oriented code, it encountered a huge obstacle in realizing multi-threading. Sure enough, the foundation has not been laid well, and the road ahead will be difficult. Due to the lack of time, the code that was rewritten in a hurry would crash as soon as it was run, and there was no time to debug, so this assignment was not submitted, and it was also my first invalid assignment.

sixth assignment


  The sixth assignment said goodbye to the elevator and asked us to implement a document scanner. After my last experience that didn't work, I told myself: I have to write it out this time anyway. Teacher Wu Ji said before that the last homework was the most difficult, and the difficulty of the next homework will be easier every time. I don't know if Wu Ji is comforting us or what. At least the first time I finished reading the instruction book for this assignment, I felt confused, and after reading it, I didn't know what it wanted us to do. I'm even worried that it won't work again this time. Later, I calmed down and read it twice, and I found that this time the problem was easier to think about than before. It requires us to monitor changes to specified files and then log the changes as required. So this job only needs one thread per request. The collaboration of threads this time is mainly reflected in the acquisition of file status. The files and directories within the monitoring range are shared resources among various monitors, so the issue of thread safety will be involved. The synchronization control method is to establish a thread-safe file class. . This guide also recommends that we design a snapshot to save the status of each monitoring object. Every time we modify the monitoring object, we compare it with the corresponding snapshot, and then update the snapshot. Since the idea is clearer this time, it is faster to implement.

  This assignment I wrote six. They are Count class, MonitorThread class, SafeFile class, Snapshot class, TestThread class, Wenjiansaomiao class. The Count class is a counter that records how many times the four triggers are triggered; the MonitorThread class is used to monitor the monitoring object and perform corresponding tasks; the SafeFile class is used to modify the file; the TestThread class is the test thread.

  The last bug reported is because I didn't consider the case where the monitoring object is a directory. The code of the classmate I got has a similar problem.

 

The seventh homework


  The seventh homework is the first homework in the taxi series. According to the seniors, there is no need to stay up late for the next homework. But I was still too weak to feel how easy this assignment was. I feel that this job is very similar to a multi-threaded elevator. It may be because the multi-threaded elevator has not been written, and the realization of this job is also more difficult. But it finally came true. Thread coordination is the coordination between the controller and the output and controlled content. The synchronization control method is to lock the method and sleep the thread.

  This assignment I wrote a total of six classes. In addition to the built-in gui class and Main class, I also wrote four classes: Command, QueueThread, TaxiThread, and Schedule. The Command class is a request class, which includes the request place, destination, time, and content; QueueThread is a request queue class, in which the input is implemented, which contains the dynamic array of requests; TaxiThread class is a taxi class, and the member variables are Number, status, reputation, and location coordinates, four state transitions are implemented in the run method; Schedule class is a scheduler class that determines which taxi is selected after a request is sent.

  最后报出来的bug是出租车每走一格不是严格的200ms。其中的原因是出租车每运行一格我就会输出系统当前时间,但是程序运行也是需要时间的,所以输出的时间间隔大于200ms。我找到的别人的bug是他的出租车在等待状态下的运行不是随机的,会在两个点之间鬼畜。一开始我是通过gui界面观察到的,但是这不能作为判定为bug的依据,所以我在他的出租车类里加了一条输出,如果出租车编号是2,输出它的位置坐标。果然他的出租车的位置在两个点之间鬼畜。

 

总结


  这三次作业全部都是多线程以及线程安全方面的问题。这三次作业让我感受到一开始有一个好的架构很重要,我现在总是想着先把大体的写出来细节问题在缝缝补补。但是有的时候当你基本功能实现完成后,想要补救一些东西是很难做到的。这三次作业完成情况总体来说不理想。但从第五次作业未完成,到第六次作业差不多完成到第七次作业基本上完成,虽然说这三次作业难度在递减,但我觉得也是一种安慰吧。最后三次作业,一定要更加努力,做到一次比一次完美。

 

Guess you like

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