gpt4 book ai didi

java - JCIP 书中定义的 core java 内部 protected 对象的示例

转载 作者:行者123 更新时间:2023-12-01 19:03:23 26 4
gpt4 key购买 nike

JCIP将 protected 对象定义为:

Guarded. A guarded object can be accessed only with a specific lock held. Guarded objects include those that are encapsulated within other thread-safe objects and published objects that are known to be guarded by a specific lock.

哪个可以作为核心 Java 内部此类对象的示例?

最佳答案

我立即想到的是 Collections.synchronizedList() 返回的 SynchronizedList 实例所保存的列表。这是其源代码的一部分:

static class SynchronizedList<E>
extends SynchronizedCollection<E>
implements List<E> {

final List<E> list;

public boolean equals(Object o) {
synchronized (mutex) {return list.equals(o);}
}
public int hashCode() {
synchronized (mutex) {return list.hashCode();}
}

public ListIterator<E> listIterator() {
return list.listIterator(); // Must be manually synched by user
}

list 对象不是线程安全的,但由互斥体保护。 listIterator() 返回的对象应该由相同的互斥体(即同步列表本身)手动保护

关于java - JCIP 书中定义的 core java 内部 protected 对象的示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11310450/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com