当前位置:网站首页>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
边栏推荐
- Kingdee EAS "general ledger" system calls "de posting" button
- Opensips (1) -- detailed process of installing opensips
- Get the value of state in effects in DVA
- JDBC operation transaction
- Traitement des séquelles du flux de Tensor - exemple simple d'enregistrement de torche. Utils. Données. Dataset. Problème de dimension de l'image lors de la réécriture de l'ensemble de données
- Chapter 3 of linear algebra - Elementary Transformation of matrix and system of linear equations
- Linear algebra Chapter 2 - matrices and their operations
- 线性代数第三章-矩阵的初等变换与线性方程组
- Denoising paper - [noise2void, cvpr19] noise2void learning denoising from single noise images
- Pyemd installation and simple use
猜你喜欢
无监督去噪——[TMI2022]ISCL: Interdependent Self-Cooperative Learning for Unpaired Image Denoising
Font shape `OMX/cmex/m/n‘ in size <10.53937> not available (Font) size <10.95> substituted.
Pytoch -- data loading and processing
Development environment EAS login license modification
Comparative study paper - [Moco, cvpr2020] momentum contract for unsupervised visual representation learning
Pytorch notes - observe dataloader & build lenet with torch to process cifar-10 complete code
Reading of denoising papers - [cvpr2022] blind2blind: self supervised image denoising with visible blind spots
Generate excel template (drop-down selection, multi-level linkage)
PyQt5学习(一):布局管理+信号和槽关联+菜单栏与工具栏+打包资源包
Solve the error: importerror: iprogress not found Please update jupyter and ipywidgets
随机推荐
Pyqy5 learning (4): qabstractbutton + qradiobutton + qcheckbox
C3p0 database connection pool usage
The official website of UMI yarn create @ umijs / UMI app reports an error: the syntax of file name, directory name or volume label is incorrect
Exception handling: grab and throw model
线代第四章-向量组的线性相关
A general U-shaped transformer for image restoration
Pytorch learning record (7): skills in processing data and training models
Chapter 4 of line generation - linear correlation of vector systems
Problems and solutions of database migration
Custom exception class
Pytorch Learning record (XIII): Recurrent Neural Network
在Jupyter notebook中用matplotlib.pyplot出现服务器挂掉、崩溃的问题
Font shape `OMX/cmex/m/n‘ in size <10.53937> not available (Font) size <10.95> substituted.
Ptorch learning record (XIII): recurrent neural network
JDBC connection database
LDCT图像重建论文——Eformer: Edge Enhancement based Transformer for Medical Image Denoising
Implementation of displaying database pictures to browser tables based on thymeleaf
Explain of MySQL optimization
The attendance client date of K / 3 wise system can only be selected to 2019
SQL injection