直接开启30个线程往集合中添加数据
package com.atguigu.lock;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;
/**
* List集合线程不安全
*/
public class ThreadDemo4 {
public static void main(String[] args) {
//创建ArrayList
List<String> list = new ArrayList<>();
for (int i = 0; i <30 ; i++) {
new Thread(()->{
//向集合添加内容
list.add(UUID.randomUUID().toString().substring(0,8));
//从集合获取内容
System.out.println(list);
},String.valueOf(i)).start();
}
}
}
可以看到,引发了线程安全问题
public class ThreadDemo4 {
public static void main(String[] args) {
//创建ArrayList
List<String> list = new Vector<>();
for (int i = 0; i <30 ; i++) {
new Thread(()->{
//向集合添加内容
list.add(UUID.randomUUID().toString().substring(0,8));
//从集合获取内容
System.out.println(list);
},String.valueOf(i)).start();
}
}
}
public class ThreadDemo4 {
public static void main(String[] args) {
//创建ArrayList
List<String> list =Collections.synchronizedList(new ArrayList<>());
for (int i = 0; i <30 ; i++) {
new Thread(()->{
//向集合添加内容
list.add(UUID.randomUUID().toString().substring(0,8));
//从集合获取内容
System.out.println(list);
},String.valueOf(i)).start();
}
}
}
```java
public class ThreadDemo4 {
public static void main(String[] args) {
//创建ArrayList
List<String> list =new CopyOnWriteArrayList<>();
for (int i = 0; i <30 ; i++) {
new Thread(()->{
//向集合添加内容
list.add(UUID.randomUUID().toString().substring(0,8));
//从集合获取内容
System.out.println(list);
},String.valueOf(i)).start();
}
}
}
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;
/**
* List集合线程不安全
*/
public class ThreadDemo4 {
public static void main(String[] args) {
//演示HashSet线程不安全
Set<String> set = new HashSet<>();
//解决方案:CopyOnWriteArraySet
// Set<String> set = new CopyOnWriteArraySet<>();
for (int i = 0; i <30 ; i++) {
new Thread(()->{
//向集合添加内容
set.add(UUID.randomUUID().toString().substring(0,8));
//从集合获取内容
System.out.println(set);
},String.valueOf(i)).start();
}
}
}
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;
/**
* List集合线程不安全
*/
public class ThreadDemo4 {
public static void main(String[] args) {
//演示HashSet线程不安全
// Set<String> set = new HashSet<>();
//解决方案:CopyOnWriteArraySet
Set<String> set = new CopyOnWriteArraySet<>();
for (int i = 0; i <30 ; i++) {
new Thread(()->{
//向集合添加内容
set.add(UUID.randomUUID().toString().substring(0,8));
//从集合获取内容
System.out.println(set);
},String.valueOf(i)).start();
}
}
}
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;
/**
* List集合线程不安全
*/
public class ThreadDemo4 {
public static void main(String[] args) {
//演示HashMap
Map<String,String> map = new HashMap<>();
for (int i = 0; i <30 ; i++) {
String key=String.valueOf(i);
new Thread(()->{
//向集合添加内容
map.put(key,UUID.randomUUID().toString().substring(0,8));
//从集合获取内容
System.out.println(map);
},String.valueOf(i)).start();
}
}
}
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;
/**
* List集合线程不安全
*/
public class ThreadDemo4 {
public static void main(String[] args) {
//演示HashMap
Map<String,String> map = new ConcurrentHashMap<>();
for (int i = 0; i <30 ; i++) {
String key=String.valueOf(i);
new Thread(()->{
//向集合添加内容
map.put(key,UUID.randomUUID().toString().substring(0,8));
//从集合获取内容
System.out.println(map);
},String.valueOf(i)).start();
}
}
}