首页 技术 正文
技术 2022年11月10日
0 收藏 612 点赞 3,825 浏览 2300 个字

线程安全同步容器(使用 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<>()) ;

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,077
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,552
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,400
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,813
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,894