Shurov lab-02#41
Conversation
| Tunnel tunnel = new Tunnel(3); | ||
|
|
||
| ShipGenerator shipGenerator = new ShipGenerator(tunnel); | ||
| Thread shipGeneratorThread = new Thread(shipGenerator); | ||
|
|
||
| Integer dockAmount = 4; |
There was a problem hiding this comment.
Все переменные должны задаваться в файле config.json. Путь до этого файла передается аргументов в вашу программу
| 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"); | ||
| } | ||
| } |
There was a problem hiding this comment.
Тут могут возникнуть гонки из-за того что этот метод не синхронизирован, и происходит работа из нескольких потоков с tunnel_.
| @Override | ||
| public void run() { | ||
| while (true) { | ||
| Random random = new Random(); |
There was a problem hiding this comment.
Зачем на каждую итерацию цикла создавать новый инстанс рандома?
| if (storageCapacity_.get(unloadingShip_.getCargoType()) + unloadingAmountPerSecond_ < maximumStorageCapacity_.get(unloadingShip_.getCargoType())) { | ||
| storageCapacity_.put(unloadingShip_.getCargoType(), storageCapacity_.get(unloadingShip_.getCargoType()) + canBeUnloaded); | ||
| } |
There was a problem hiding this comment.
Тут же может произойти зацикливание, если товара больше чем нужно и его никак не смогту разгрузить, то сюда не сможет зайти другой корабаль.
| @Override | ||
| public void run() { | ||
| for (int i = 0; i < hobos_.size(); ++i) { | ||
| Thread hubbabubba = new Thread(hobos_.get(i)); |
|
|
||
| boolean allIngredientsCollected() { | ||
| for (String ingredient : portionIngredients_.keySet()) { | ||
| if ((double)portionIngredients_.get(ingredient) != (double)collectedIngredients.get(ingredient)) { |
| HobosCamp hobosCamp_; | ||
|
|
||
| public class HobosCamp implements Runnable { | ||
| HashMap<String, Double> portionIngredients_ = new HashMap<>(); |
| @Override | ||
| public void run() { | ||
| while (true) { | ||
| synchronized (this) { |
There was a problem hiding this comment.
Зачем здесь синхронизация по this? С этим классом ты нигде больше не работаешь.
| if (!allIngredientsCollected()) { | ||
| for (String ingredient : portionIngredients_.keySet()) { | ||
| if (portionIngredients_.get(ingredient) - collectedIngredients.get(ingredient) != 0) { | ||
| collectedIngredients.put(ingredient, collectedIngredients.get(ingredient) + 1); |
There was a problem hiding this comment.
А вот тут уже могут быть гонки между collectedIngredients.put и collectedIngredients.get, volatile здесь не поможет.
|
Основная оценка:
Допы:
В итоге: |
Лучшая работа в мире! (мем)