同步容器类
- 此种同步容器类(synchronized开头的方法)在底层将每种add,remove等方法前加上了synchronized修饰。
- 1.在同步容器中,这些复合操作在没有客户端加锁的情况下仍然是线程安全的,但当其他线程并发地修改容器时,它们可能会表现出意料之外的行为。
- 2.可以通过在客户端加锁来解决不可靠迭代的问题,但要牺牲一些伸缩性。这同样会导致其他线程在迭代器件无法访问它,因此降低了并发性。
- 应用时其他复合操作需要加锁解决,迭代操作会导致效率问题。且迭代时修改会报错ConcurrentModificationException。
- 如没有复合操作,迭代等,此方法比较方便。
- 如果同时有并发线程修改容器时,或一些复合操作,考虑ConcurrentHashMap等同步工具类
下面是我的代码小示例:
public class SyncList {
private final List<Integer> list;
public SyncList() {
this.list = Collections.synchronizedList(new ArrayList<Integer>());
}
//添加元素
public void add(Integer data) {
list.add(data);
}
//排序
public void sortList() {
list.sort(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
int result = o1 - o2;
if(result > 0) {
return 1;
}else if(result == 0) {
return 0;
}else {
return -1;
}
}
});
}
public List<Integer> getList() {
return list;
}
}
package com.cc.synchronizedClass;
/**
* @ClassName: Test
* @Description: 模拟多线程环境测试
* @author CC
* @date 2018年11月21日 下午3:28:21
* @version V1.0
*/
public class Test {
public static void main(String[] args) {
SyncList syncList = new SyncList();
Runnable runnableA = new Runnable() {
@Override
public void run() {
while(true) {
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
Integer data = (int)(Math.random() * 100);
syncList.add(data);
System.out.println("syncList.add(" + data + ")");
}
}
};
Runnable runnableB = new Runnable() {
@Override
public void run() {
while(true) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
syncList.sortList();
System.out.println("syncList.sortList()-->" + syncList.getList());
}
}
};
Thread threadA = new Thread(runnableA);
threadA.setName("threadA");
threadA.start();
Thread threadB = new Thread(runnableB);
threadB.setName("threadB");
threadB.start();
}
}
运行结果(截取部分):
syncList.add(8)
syncList.sortList()–>[8]
syncList.add(2)
syncList.add(46)
syncList.sortList()–>[2, 8, 46]
syncList.add(23)
syncList.add(58)
syncList.sortList()–>[2, 8, 23, 46, 58]
syncList.add(13)
syncList.sortList()–>[2, 8, 13, 23, 46, 58]
syncList.add(26)
syncList.add(28)
syncList.sortList()–>[2, 8, 13, 23, 26, 28, 46, 58]
syncList.add(16)
syncList.add(73)
syncList.sortList()–>[2, 8, 13, 16, 23, 26, 28, 46, 58, 73]
syncList.add(82)
syncList.sortList()–>[2, 8, 13, 16, 23, 26, 28, 46, 58, 73, 82]
syncList.add(29)
syncList.add(5)
syncList.sortList()–>[2, 5, 8, 13, 16, 23, 26, 28, 29, 46, 58, 73, 82]
syncList.add(25)
syncList.add(51)
syncList.sortList()–>[2, 5, 8, 13, 16, 23, 25, 26, 28, 29, 46, 51, 58, 73, 82]
syncList.add(21)
syncList.sortList()–>[2, 5, 8, 13, 16, 21, 23, 25, 26, 28, 29, 46, 51, 58, 73, 82]
syncList.add(83)
syncList.add(64)
syncList.sortList()–>[2, 5, 8, 13, 16, 21, 23, 25, 26, 28, 29, 46, 51, 58, 64, 73, 82, 83]
syncList.add(14)
syncList.add(2)
syncList.sortList()–>[2, 2, 5, 8, 13, 14, 16, 21, 23, 25, 26, 28, 29, 46, 51, 58, 64, 73, 82, 83]
syncList.add(48)
syncList.sortList()–>[2, 2, 5, 8, 13, 14, 16, 21, 23, 25, 26, 28, 29, 46, 48, 51, 58, 64, 73, 82, 83]
syncList.add(0)
syncList.add(74)
syncList.sortList()–>[0, 2, 2, 5, 8, 13, 14, 16, 21, 23, 25, 26, 28, 29, 46, 48, 51, 58, 64, 73, 74, 82, 83]
syncList.add(24)
syncList.add(41)
syncList.sortList()–>[0, 2, 2, 5, 8, 13, 14, 16, 21, 23, 24, 25, 26, 28, 29, 41, 46, 48, 51, 58, 64, 73, 74, 82, 83]
syncList.add(98)
syncList.sortList()–>[0, 2, 2, 5, 8, 13, 14, 16, 21, 23, 24, 25, 26, 28, 29, 41, 46, 48, 51, 58, 64, 73, 74, 82, 83, 98]
。。。。。。