当前位置:网站首页>Collection and map thread safety problem solving
Collection and map thread safety problem solving
2022-04-23 06:09:00 【linsa_ pursuer】
1.ArrayList Thread unsafe ( There is no reentrant lock ) resolvent Vector(synchronized) Collections(synchronized) CopyOnWriteArrayList(ReentrantLock)
2.HashSet——》CopyOnWriteArraySet
3.HashMapt——》ConcurrentHashMap
package juc.two;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;
/**
* ArrayList Thread unsafe ( There is no reentrant lock )
* resolvent Vector(synchronized) Collections(synchronized) CopyOnWriteArrayList(ReentrantLock)
*/
public class ThreadDemo4 {
public static void main(String[] args) {
// establish ArrayList aggregate
//List<String> list = new ArrayList<>();
//Vector solve
//List<String> list = new Vector<>();
//Collections solve
//List<String> list = Collections.synchronizedList(new ArrayList<>());
//CopyOnWriteArrayList solve
//List<String> list = new CopyOnWriteArrayList<>();
/*for(int i = 0; i<30; i++){
new Thread(()->{
// Add content to collection
list.add(UUID.randomUUID().toString().substring(0,8));
// Get content from collection
System.out.println(list);
},String.valueOf(i)).start();
}*/
// demonstration HashSet
//Set<String> set = new HashSet<>();
//Set<String> set = new CopyOnWriteArraySet<>();
/*for(int i = 0; i<30; i++){
new Thread(()->{
// Add content to collection
set.add(UUID.randomUUID().toString().substring(0,8));
// Get content from collection
System.out.println(set);
},String.valueOf(i)).start();
}*/
// demonstration HashMap
//Map<String,String> map = new HashMap<>();
Map<String,String> map = new ConcurrentHashMap<>();
for(int i = 0; i<30; i++){
String key = String.valueOf(i);
new Thread(()->{
// Add content to collection
map.put(key,UUID.randomUUID().toString().substring(0,8));
// Get content from collection
System.out.println(map);
},String.valueOf(i)).start();
}
}
}
版权声明
本文为[linsa_ pursuer]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220533259314.html
边栏推荐
- Pytorch学习记录(三):神经网络的结构+使用Sequential、Module定义模型
- Viewer: introduce MySQL date function
- 線性代數第二章-矩陣及其運算
- Automatic control (Han min version)
- 线性代数第一章-行列式
- Reading of denoising paper - [ridnet, iccv19] real image denoising with feature attention
- On traversal of binary tree
- Latex quick start
- Pytorch notes - get familiar with the network construction method by building RESNET (complete code)
- 在Jupyter notebook中用matplotlib.pyplot出现服务器挂掉、崩溃的问题
猜你喜欢
Fundamentals of digital image processing (Gonzalez) I
数字图像处理基础(冈萨雷斯)二:灰度变换与空间滤波
Denoising paper - [noise2void, cvpr19] noise2void learning denoising from single noise images
線性代數第二章-矩陣及其運算
Pytorch学习记录(十一):数据增强、torchvision.transforms各函数讲解
编程记录——图片旋转函数scipy.ndimage.rotate()的简单使用和效果观察
PyTorch笔记——观察DataLoader&用torch构建LeNet处理CIFAR-10完整代码
Solve the error: importerror: iprogress not found Please update jupyter and ipywidgets
Graphic numpy array matrix
去噪论文阅读——[RIDNet, ICCV19]Real Image Denoising with Feature Attention
随机推荐
Pytorch learning record (IX): convolutional neural network in pytorch
5.The Simple Problem
1. Calculate a + B
Create enterprise mailbox account command
Pyqy5 learning (4): qabstractbutton + qradiobutton + qcheckbox
Illustrate the significance of hashcode
8. Integer Decomposition
Anaconda
Pytorch学习记录(四):参数初始化
MySQL basic madness theory
MySQL best practices for creating tables
DBCP usage
Algèbre linéaire chapitre 2 - matrice et son fonctionnement
Fundamentals of in-depth learning -- a simple understanding of meta learning (from Li Hongyi's course notes)
The user name and password of users in the domain accessing the samba server outside the domain are wrong
Explain of MySQL optimization
Protected (members modified by protected are visible to this package and its subclasses)
Pytorch学习记录(十一):数据增强、torchvision.transforms各函数讲解
Write your own redistemplate
Delete and truncate