Software Construction Lab6 experience

Class design ideas

1.Ladder categories: storing the current status of all ladders, monkey any objects on the pedal, i.e., no monkeys was null. Ladder objects in a monkey across the river created only once, and objects visible to all monkeys, and a total
share
2.Monkey categories: store information about monkeys, each object can be used as a running thread
Monkey object will call the Strategy Interface sub-categories, namely calling tactics
3.Strategy class: monkey storage strategy by Strategy1,2,3 components. All Strategy (Strategy1, Strategy2, Strategy3) is to improve the interface of the Strategy of
4.MonkeyGenerator: ladder that is responsible for generating the desired target monkey experiments while changing the parameters, obtaining experimental data

run () execution flow ideas

Here ladder without considering the specific use of selection policy. Monkeys Monkeys thread starts representatives have produced
step1: printing and related information log monkey
step2: Each time the decision to start the monkey from the time of birth + 1s. When the monkeys on the ladder yet, that is, in situ observation state, the need for policy options. After obtaining the results of the decision, the monkeys began to try to move, this time to generate competition among monkeys, the need for thread synchronization, thread lock and unlock at the end of the movement. If the monkey is already on the ladder, you do not need a ladder policy decision, based on the speed of the monkey, monkey see if you can turn on the pedal can be reached; when the monkeys reach the other side, out of the loop, the end of the thread.

Note:
1. move the start to be locked to unlocked at the end of
2 monkeys to reach the last piece of the pedal, we need to pause 1s, waiting for a decision.

Strategies

Strategy 1
design:
preference without a ladder monkey, if all ladders are monkeys, then there is no preference for a ladder with monkeys to me and rows; if the condition is satisfied there are a lot of ladders, the random selection.
Implementation:
traversing all pedals all ladders, according to the ladder, get (param1, param2) determines whether the return value of a piece of the pedal monkeys, monkeys did not give the ladder. If all monkeys are ladder, according
ladder, get (param1, param2) returns a copy of the object on the ladder monkey, Monkey class method of recycling getDirection traveling direction obtained monkey, monkey no relative judgment obtained by the line of ladder.
If the ladder does not satisfy the above conditions, -1, representing no ladder Alternatively, continue to wait to observe.

Strategy 2
Design:
preference fastest overall progress of the ladder (monkey is not with me to the line for the (condition 1), the ladder away from the real speed of travel I recently monkey fastest (Condition 2))
implementations:
Priority Search condition 1 is satisfied, if a plurality, then according to condition 2, the selection of a more appropriate ladder returns,
if the condition of the same binary plurality ladder, randomly returns. 1 without satisfying the condition of the ladder, according to the condition
2 selection, if the condition of the same binary plurality ladder, randomly returns.

How to ensure threadsafe?

1. ADT - Ladder, since the Monkey thread will Ladder objects to read and write, therefore Ladder will be the thread to read and write attributes ladders require the use of thread-safe packaging
2. when the Monkey thread of execution, since a plurality of Monkey It will be shared between threads a Ladder objects, so when the thread Ladder objects to read and write easily lead to competition problems, and therefore the implementation, in place, you need to lock and unlock.

Output Design

Use log output: each experiment and printed first recording parameter, monkeys thread starts execution, printing attribute value monkey, monkey for printing from birth every second mobile will and recording logger class java.util package. achieve.

Guess you like

Origin blog.csdn.net/jason1034538932/article/details/92398582