Skip to content

Conversation

@manfe513
Copy link
Owner

@manfe513 manfe513 commented Jun 3, 2025

No description provided.

Васильев Андрей Георгиевич added 3 commits June 3, 2025 19:44
}

public long getLastValue() {
return lastValue;

Choose a reason for hiding this comment

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

одного synchronized не достаточно.
Тут он тоже нужен.

Choose a reason for hiding this comment

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

как реализуется требование ДЗ ?

Число, полученное от сервера должно учитываться только один раз.

Copy link
Owner Author

@manfe513 manfe513 Jun 4, 2025

Choose a reason for hiding this comment

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

++

  1. по synchronized упустил, добавил сюда тоже
  2. про реализацию требования: реализовано в NumsClient через lastServerValue в методе calculateNextValue проверка что одинаковое значение не использовать повторно

Если точнее - https://github.com/manfe513/java-otus/pull/15/files#diff-e3224650e63e135729d99535f01823245d730cd579fdd3aaf32b30305bb4bcd8R44

Вот на примере выдачи:

image

Choose a reason for hiding this comment

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

не вижу в коде добавленный synchnonized

Copy link
Owner Author

Choose a reason for hiding this comment

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

не запушилось по-видимому не углядел
сейчас убрал, сделал по совету с AtomicLong

}

public long getLastValue() {
return lastValue;

Choose a reason for hiding this comment

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

не вижу в коде добавленный synchnonized


long nextVal = currentValue;

if (serverValue != lastServerValue && serverValue > 0) {

Choose a reason for hiding this comment

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

это условие
serverValue > 0
кажется тут лишним.

Copy link
Owner Author

Choose a reason for hiding this comment

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

++


long nextVal = currentValue;

if (serverValue != lastServerValue && serverValue > 0) {

Choose a reason for hiding this comment

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

сравнивать значение с предыдущим не очень надежно.
Т.к. значение может задвоиться и его пропустите.
Надежнее и проще использовать AtomicLong.getAndSet(0)
тогда и synchronized будет не нужен и логика заметно упростится.

Copy link
Owner Author

Choose a reason for hiding this comment

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

++
ого, благодарю, не подумал так даже

Copy link
Owner Author

Choose a reason for hiding this comment

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

убрал synchronized
сделал с AtomicLong

long serverValue = streamObserver.getLastValue();
logger.info("serverValue: {}", currentValue);

long previousServerValue = lastServerValue.getAndSet(serverValue);

Choose a reason for hiding this comment

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

тут проще можно:
currentValue + lastServerValue.getAndSet(0) + 1;

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.

3 participants