how to create Synchronized arraylist

前端 未结 7 1468
轻奢々
轻奢々 2020-12-15 07:50

i have created synchronized arrayList like this

import java.text.SimpleDateFormat;
import java.util.*;


class HelloThread  
{

 int i=1;
 List arrayList;
           


        
7条回答
  •  旧巷少年郎
    2020-12-15 08:44

    Consider using a CopyOnWriteArrayList which is thread-safe. Every time you add an item, a fresh copy of the underlying array is created. However, the iterator will not reflect additions to the list since the iterator was created, but is guaranteed not to throw ConcurrentModificationException.

    arrayList=new CopyOnWriteArrayList();
    

提交回复
热议问题