powered by simpleCommunicator - 2.0.61     © 2026 Programmizd 02
Целевая тема:
Создать новую тему:
Автор:
Закрыть
Цитировать
Форумы / Java [игнор отключен] [закрыт для гостей] / Вопрос по concurrency in pratctise
3 сообщений из 3, страница 1 из 1
Вопрос по concurrency in pratctise
    #39398300
questioner
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
Начал читать с середины. Про безопасную публикацию

3.5. Safe Publication
So far we have focused on ensuring that an object not be published, such as when it is supposed to be confined to a thread or within another object. Of course, sometimes we do want to share objects across threads, and in this case we must do so safely. Unfortunately, simply storing a reference to an object into a public field, as in Listing 3.14, is not enough to publish that object safely.

Listing 3.13. Caching the Last Result Using a Volatile Reference to an Immutable Holder Object.

Код: java
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
@ThreadSafe
public class VolatileCachedFactorizer implements Servlet {
    private volatile OneValueCache cache =
            new OneValueCache(null, null);

    public void service(ServletRequest req, ServletResponse resp) {
        BigInteger i = extractFromRequest(req);
        BigInteger[] factors = cache.getFactors(i);
        if (factors == null) {
            factors = factor(i);
            cache = new OneValueCache(i, factors);
        }
        encodeIntoResponse(resp, factors);
    }


}

Listing 3.14. Publishing an Object without Adequate Synchronization. Don't Do this.
Код: java
1.
2.
3.
4.
5.
6.
// Unsafe publication
public Holder holder;

public void initialize() {
holder = new Holder(42);
}



You may be surprised at how badly this harmless-looking example could fail. Because of visibility problems, the Holder could appear to another thread to be in an inconsistent state, even though its invariants were properly established by its constructor! This improper publication could allow another thread to observe a partially constructed object.

Возникло пару вопросов по книге.

Что за аннотация @ThreadSafe ?

код(3.13) в главе представлен, но ссылок на него ниоткуда нет. Зачем он тогда представлен?
...
Рейтинг: 0 / 0
Вопрос по concurrency in pratctise
    #39398305
Сергей Арсеньев
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
questionerЧто за аннотация @ThreadSafe ?
Что-нибудь типа .

Ну видел как иронию в тексте выделяют курсивом. Или прямую речь кавычками.
Вот автор специально для тебя поясняет, что приведенный код с его точки зрения поткобезопасный.
...
Рейтинг: 0 / 0
Вопрос по concurrency in pratctise
    #39398405
questioner
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
Сергей Арсеньев,

я понял, эта картинка к другому разделу. Очень странно книга устроена.

раздел 3.5 меня разочаровал. Один пример неправильной публикации и всё. примеров правильной нет. Никаких разборов.
...
Рейтинг: 0 / 0
3 сообщений из 3, страница 1 из 1
Форумы / Java [игнор отключен] [закрыт для гостей] / Вопрос по concurrency in pratctise
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]