由于Set、List和Map都是线程不安全的,为了同步控制,Collections类提供了多个synchronizedXxx()方法,该方法可以将指定集合包装成线程同步的集合,从而可以解决多线程并发访问集合时的线程安全问题,例如:

  

 1 public class Test {
 2 
 3     public static void main(String[] args) {
 4         
 5          Collection c=Collections.synchronizedCollection(new ArrayList());
 6          List list=Collections.synchronizedList(new ArrayList());
 7          Set set=Collections.synchronizedSet(new HashSet());
 8          Map map=Collections.synchronizedMap(new HashMap());
 9     }
10 }

 

相关文章:

  • 2021-05-29
  • 2021-10-16
  • 2021-09-01
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-17
猜你喜欢
  • 2022-12-23
  • 2021-11-17
  • 2022-01-02
  • 2021-11-15
  • 2022-01-10
相关资源
相似解决方案