Skip to content

Hw34 grpc#15

Open
manfe513 wants to merge 4 commits into
mainfrom
hw34-grpc
Open

Hw34 grpc#15
manfe513 wants to merge 4 commits into
mainfrom
hw34-grpc

Conversation

@manfe513

@manfe513 manfe513 commented Jun 3, 2025

Copy link
Copy Markdown
Owner

No description provided.

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

public long getLastValue() {
return lastValue;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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

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

@manfe513 manfe513 Jun 4, 2025

Copy link
Copy Markdown
Owner Author

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
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;

Copy link
Copy Markdown

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) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
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) {

Copy link
Copy Markdown

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
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
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);

Copy link
Copy Markdown

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.

2 participants