반응형
리스트
private List<String> forceInsertItemCdList = Collections.synchronizedList(new ArrayList<>());
내부 코드
/**
* Returns a synchronized (thread-safe) list backed by the specified
* list. In order to guarantee serial access, it is critical that
* <strong>all</strong> access to the backing list is accomplished
* through the returned list.<p>
*
* It is imperative that the user manually synchronize on the returned
* list when iterating over it:
* <pre>
* List list = Collections.synchronizedList(new ArrayList());
* ...
* synchronized (list) {
* Iterator i = list.iterator(); // Must be in synchronized block
* while (i.hasNext())
* foo(i.next());
* }
* </pre>
* Failure to follow this advice may result in non-deterministic behavior.
*
* <p>The returned list will be serializable if the specified list is
* serializable.
*
* @param <T> the class of the objects in the list
* @param list the list to be "wrapped" in a synchronized list.
* @return a synchronized view of the specified list.
*/
public static <T> List<T> synchronizedList(List<T> list) {
return (list instanceof RandomAccess ?
new SynchronizedRandomAccessList<>(list) :
new SynchronizedList<>(list));
}
선언하면 내부적으로 동기화된 리스트로 선언하도록 구성되어 있음.
더 자세하고 여러 콜렉션에 대해 보고 싶으면 아래 두 개 보면 됨
https://dveamer.github.io/backend/JavaConcurrentCollections.html
Dveamer
현실에서 살고 있지만 이상에 대한 꿈을 버리지 못한 몽상가의 홈페이지 입니다. 개인적인 기록을 주 목적으로 하며 일상과 프로그래밍 관련 글을 포스팅합니다.
dveamer.github.io
https://cornswrold.tistory.com/209
[JAVA] 동기화된 컬렉션(thread-safe collection), 병렬처리 가능한 컬렉션
동기화된 컬렉션(thread-safe한 collection), 병렬처리 컬렉션 컬렉션 프레임워크 대부분 싱글 스레드 환경에서 사용할 수 있도록 설계Vector, Hashtable은 동기화된 (synchronized) 메소드로 구성되어 있기 때
cornswrold.tistory.com
반응형
댓글