Skip to content

Conversation

@dkaznacheev
Copy link
Owner

No description provided.

public T get() {
if (!counted) {
counted = true;
result = supplier.get();

Choose a reason for hiding this comment

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

Так supplier оказывается в замыкании и ссылка на него хранится даже когда он уже использован по назначению. Правильнее сделать supplier полем и чтобы он не попадал в замыкание

}

@Test
public void multiThreadLazyShortTest() throws InterruptedException{

Choose a reason for hiding this comment

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

Пробела не хватает :)

}
assertTrue(job.calledOnce());
}
}

Choose a reason for hiding this comment

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

Кажется, нигде не проверяется, что lazy.get вообще возвращает что-то разумное, и что это каждый раз один и тот же объект

return new Lazy<T>() {
private Supplier<T> lazySupplier = supplier;
private T result;
private boolean counted = false;

Choose a reason for hiding this comment

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

Теперь тут три поля, а надо два. counted, вообще говоря, не нужен, по состоянию других полей можно понять, вызывали мы supplier или нет

public T get() {
if (!counted) {
counted = true;
result = lazySupplier.get();

Choose a reason for hiding this comment

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

Надо сбросить ссылку на supplier, когда мы уже получили значение и он нам не нужен, чтобы сборщик мусора смог его собрать

if (value == notInitialized) {
synchronized (supplier) {
if (value == notInitialized) {
value = supplier.get();

Choose a reason for hiding this comment

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

Тут тоже, надо аккуратно избавиться от ссылки на supplier.

if (!isLong)
return getShort();
else
return getLong();

Choose a reason for hiding this comment

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

Кажется, тут лучше тернарный оператор --- это же просто выражение, значение которого зависит от условия

Copy link

@yurii-litvinov yurii-litvinov left a comment

Choose a reason for hiding this comment

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

Да, теперь всё ок, зачтена

@@ -0,0 +1,751 @@
<?xml version="1.0" encoding="UTF-8"?>

Choose a reason for hiding this comment

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

Оо, выложить .idea/workspace.xml --- это что-то вроде самоубийства :) .gitignore сломался?

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