线程安全同步容器(使用 synchronized关键字)

1.ArrayList->Vector,Stack

2.HashMap->HashTable(key、value不能为null)

3.Collections.synchronizedXXX(List、Set、Map)

同步容器也有线程不安全的情况 stack继承vector

package com.alan.concurrency.example.syncContainer;

import com.alan.concurrency.annoations.NotThreadSafe;
import com.alan.concurrency.annoations.ThreadSafe;
import lombok.extern.slf4j.Slf4j; import java.util.Vector;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore; @Slf4j
@NotThreadSafe
public class VectorExample2 { private static Vector<Integer> vector = new Vector<>(); public static void main(String[] args) { while (true){ for (int i = 0; i < 10; i++) {
vector.add(i);
} Thread thread1 = new Thread(){
public void run(){
for (int i = 0; i < vector.size(); i++) {
vector.remove(i);
}
}
}; Thread thread2 = new Thread(){
public void run(){
for (int i = 0; i <vector.size(); i++) {
vector.get(i);
}
}
}; thread1.start();
thread2.start();
} }
}

HashTable同步容器

package com.alan.concurrency.example.syncContainer;

import com.alan.concurrency.annoations.NotThreadSafe;
import lombok.extern.slf4j.Slf4j; import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore; @Slf4j
public class HashTableExample { // 请求总数
public static int clientTotal = 5000; // 同时并发执行的线程数
public static int threadTotal = 200; private static Map<Integer, Integer> map = new Hashtable<>(); public static void main(String[] args) throws Exception {
ExecutorService executorService = Executors.newCachedThreadPool();
final Semaphore semaphore = new Semaphore(threadTotal);
final CountDownLatch countDownLatch = new CountDownLatch(clientTotal);
for (int i = 0; i < clientTotal; i++) {
final int count = i;
executorService.execute(() -> {
try {
semaphore.acquire();
update(count);
semaphore.release();
} catch (Exception e) {
log.error("exception", e);
}
countDownLatch.countDown();
});
}
countDownLatch.await();
executorService.shutdown();
log.info("size:{}", map.size());
} private static void update(int i) {
map.put(i, i);
}
}

Collections.synchronized相关同步容器

1、public static List<Integer> list = Collections.synchronizedList(new ArrayList<>()) ;
2、 public static Set<Integer> set = Collections.synchronizedSet(new HashSet<>()) ;
3、 private static Map<Integer, Integer> map = Collections.synchronizedMap(new HashMap<>()) ;

最新文章

  1. Yii1.1的验证规则
  2. mysql向表中某字段后追加一段字符串:
  3. rocketmq生产者部署的机器注意事项
  4. C# winfrom HttpWebRequest 请求获取html网页信息和提交信息
  5. UML和UP简介(转载)
  6. 线程池大小 &amp; cpu core
  7. c++标准库
  8. jQuery学习笔记:整理一些常用的jQuery操作DOM事件
  9. COGS731 [网络流24题] 最长递增子序列(最大流)
  10. [转]通过PowerShell工具跨多台服务器执行SQL脚本
  11. Oracle中的多表查询
  12. cygwin中运行命令提示command not found的解决方法
  13. 菜鸟学习Struts——总结
  14. Genymotion安卓模拟器,性能最好
  15. Centos6 下启动httpd报错 Could not reliably determine the server&#39;s解决方法
  16. java 图片高保真缩放
  17. 正则表达式&amp;常用JS校验
  18. Zabbix 监控数据库MSSqlServer
  19. Jmeter连接数据库方式
  20. java的局部变量和成员变量以及区别

热门文章

  1. hdu2825(AC 自动机)
  2. 棋盘V
  3. leetcode116 Populating Next Right Pointers in Each Node
  4. 【堆】bzoj1293 [SCOI2009]生日礼物
  5. 【神奇の做法】bzoj2456 mode
  6. 基于java过滤器实现的ajax跨域解决方案
  7. 狗日的Javascript中的闭包
  8. debian6 安装VirtualBox的方法
  9. CAD中如何裁剪需要的区域
  10. Gitlab系列八之重置管理员密码