Skip to content

Shurov lab-02#41

Open
MikhailShurov wants to merge 2 commits into
Rrenkens:mainfrom
MikhailShurov:lab-02
Open

Shurov lab-02#41
MikhailShurov wants to merge 2 commits into
Rrenkens:mainfrom
MikhailShurov:lab-02

Conversation

@MikhailShurov

Copy link
Copy Markdown

Лучшая работа в мире! (мем)

Comment on lines +12 to +17
Tunnel tunnel = new Tunnel(3);

ShipGenerator shipGenerator = new ShipGenerator(tunnel);
Thread shipGeneratorThread = new Thread(shipGenerator);

Integer dockAmount = 4;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Все переменные должны задаваться в файле config.json. Путь до этого файла передается аргументов в вашу программу

Comment on lines +17 to +26
public void addShip(Ship ship) {
if (tunnel_.size() < maxShips_) {
tunnel_.add(ship);
logger.info("Ship added to tunnel. Tunnel size = " + tunnel_.size() + "\n");
System.out.println("TUNNEL: Ship added, tunnel size: " + tunnel_.size());
} else {
logger.info("Ship successfully sank\n");
System.out.println("TUNNEL: Ship successfully sank");
}
}

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Тут могут возникнуть гонки из-за того что этот метод не синхронизирован, и происходит работа из нескольких потоков с tunnel_.

@Override
public void run() {
while (true) {
Random random = new Random();

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Зачем на каждую итерацию цикла создавать новый инстанс рандома?

Comment on lines +45 to +47
if (storageCapacity_.get(unloadingShip_.getCargoType()) + unloadingAmountPerSecond_ < maximumStorageCapacity_.get(unloadingShip_.getCargoType())) {
storageCapacity_.put(unloadingShip_.getCargoType(), storageCapacity_.get(unloadingShip_.getCargoType()) + canBeUnloaded);
}

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Тут же может произойти зацикливание, если товара больше чем нужно и его никак не смогту разгрузить, то сюда не сможет зайти другой корабаль.

@Override
public void run() {
for (int i = 0; i < hobos_.size(); ++i) {
Thread hubbabubba = new Thread(hobos_.get(i));

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?


boolean allIngredientsCollected() {
for (String ingredient : portionIngredients_.keySet()) {
if ((double)portionIngredients_.get(ingredient) != (double)collectedIngredients.get(ingredient)) {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Почему тут каст к double?

HobosCamp hobosCamp_;

public class HobosCamp implements Runnable {
HashMap<String, Double> portionIngredients_ = new HashMap<>();

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Почему тут в Double?

@Override
public void run() {
while (true) {
synchronized (this) {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Зачем здесь синхронизация по this? С этим классом ты нигде больше не работаешь.

if (!allIngredientsCollected()) {
for (String ingredient : portionIngredients_.keySet()) {
if (portionIngredients_.get(ingredient) - collectedIngredients.get(ingredient) != 0) {
collectedIngredients.put(ingredient, collectedIngredients.get(ingredient) + 1);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А вот тут уже могут быть гонки между collectedIngredients.put и collectedIngredients.get, volatile здесь не поможет.

@Rrenkens

Copy link
Copy Markdown
Owner

Основная оценка:

  • -0.5 за зацикливание.
  • -0.5 за лишний synchronized.
  • -2 за гонки.
  • -2 за неиспользование конфига.

Допы:

  • +0.5 за логирование, но нет смены по истечению времени.

В итоге:
Основная оценка – 5.
Допы – 0.5.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants