当前位置:网站首页>ArrayList多线程安全解决办法
ArrayList多线程安全解决办法
2022-07-28 05:16:00 【小疯子青】
经测试可以解决线程安全问题
public class Main {
public static volatile Boolean flag = false;
public static void main(String[] args) {
ArrayList arrayList = new ArrayList();
for (int i = 0; i < 100; i++) {
arrayList.add(Integer.toString(i));
}
Iterator i = arrayList.listIterator();
for (int x = 0; x < 100; x++) {
MyThread myThread = new MyThread(i, arrayList);
myThread.start();
}
flag = true;
}
static class MyThread extends Thread {
private Iterator i;
private ArrayList arrayList;
public MyThread(Iterator i, ArrayList arrayList) {
this.i = i;
this.arrayList = arrayList;
}
@Override
public void run() {
while (!flag) {
}
while (i.hasNext()) {
synchronized (flag) {
if (i.hasNext()) {
Object o = i.next();
System.out.println(currentThread().getName() + ":" + o + ":" + o.hashCode());
i.remove();
}
}
// try {
// sleep(10);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
}
}
}
}
运行结果:
Thread-59:89:1793
Thread-55:90:1815
Thread-63:91:1816
Thread-70:92:1817
Thread-75:93:1818
Thread-71:94:1819
Thread-74:95:1820
Thread-79:96:1821
Thread-83:97:1822
Thread-82:98:1823
Thread-86:99:1824
Process finished with exit code 0
边栏推荐
- Online sql to XML tool
- FreeRTOS learning (I)
- Barbie q! How to analyze the new game app?
- Flink mind map
- Testcafe's positioning, operation of page elements, and verification of execution results
- How to analyze fans' interests?
- Jsonp single sign on permission verification
- C language: addition and deletion of linked list in structure
- MySQL 默认隔离级别是RR,为什么阿里等大厂会改成RC?
- 【计算机三级信息安全】信息安全保障概述
猜你喜欢

Paper reading notes -- crop yield prediction using deep neural networks

C language classic 100 question exercise (1~21)

FreeRTOS learning (I)

为什么md5不可逆,却还可能被md5免费解密网站解密

Database date types are all 0

Flink mind map

CPU and memory usage are too high. How to modify RTSP round robin detection parameters to reduce server consumption?

【ARXIV2205】Inception Transformer

【ARXIV2204】Simple Baselines for Image Restoration

The research group passed the thesis defense successfully
随机推荐
Antd setfieldsvalue warning problem cannot use 'setfieldsvalue' until you use 'getfielddecorator' or
Making RPM packages with nfpm
HashSet add
2022 summer practice (first week)
Clickhouse pit filling note 2: the join condition does not support non equal judgments such as greater than and less than
11.< tag-动态规划和子序列, 子数组>lt.115. 不同的子序列 + lt. 583. 两个字符串的删除操作 dbc
mysql的日期与时间函数,varchar与date相互转换
JMeter related knowledge sorting
使用nfpm制作rpm包
Clickhouse填坑记2:Join条件不支持大于、小于等非等式判断
POJ 3728 the merchant (online query + double LCA)
PC side bug record
First acquaintance with C language (2)
类和对象【中】
JSON in JS (launch object deep copy)
【CVPR2022 oral】Balanced Multimodal Learning via On-the-fly Gradient Modulation
Win10 machine learning environment construction pycharm, anaconda, pytorch
Handling of web page image loading errors
After ruoyi generates the code corresponding to the database, what should I do to make the following image look like
regular expression