import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.LinkedBlockingQueue;
public class ConcurrectLinkedQueueDemos {
static List<String> list = new ArrayList<String>();
static ConcurrentLinkedQueue<String> queue = new ConcurrentLinkedQueue<String>();
static LinkedBlockingQueue<String> block = new LinkedBlockingQueue<String>();
public static void main(String[] args) throws InterruptedException {
System.out.println("bein");
for(int i=0;i<10;i++){
Thread thread = new Thread(new Runnable() {
public void run() {
demo();
}
});
thread.start();
thread.join();
Thread.sleep(1000);//确保全部线程都执行完毕
System.out.println("列表大小:"+list.size());
System.out.println("队列大小:"+queue.size());
System.out.println("阻塞队列大小:"+block.size());
System.out.println("end");
System.out.println("========================================================");
}
}
public static void demo(){
for (int i = 0; i < 10000; i++) {
Thread aa = new Thread(new Runnable() {
public void run() {
list.add("AA");
}
});
aa.start();
}
for (int i = 0; i < 10000; i++) {
Thread aa = new Thread(new Runnable() {
public void run() {
queue.add("aa");
}
});
aa.start();
}
for (int i = 0; i < 10000; i++) {
Thread aa = new Thread(new Runnable() {
public void run() {
try {
block.put("s");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
aa.start();
}
}
}
解决方案
使用
Collections.synchronizedList(list);
将之变换为线程安全的
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.LinkedBlockingQueue;
public class ConcurrectLinkedQueueDemos {
static List<String> list = new ArrayList<String>();
static ConcurrentLinkedQueue<String> queue = new ConcurrentLinkedQueue<String>();
static LinkedBlockingQueue<String> block = new LinkedBlockingQueue<String>();
public static void main(String[] args) throws InterruptedException {
/*使用Collections工具类将list转换为线程安全的*/
list = Collections.synchronizedList(list);
/*使用Collections工具类将list转换为线程安全的*/
System.out.println("bein");
for(int i=0;i<10;i++){
Thread thread = new Thread(new Runnable() {
public void run() {
demo();
}
});
thread.start();
thread.join();
Thread.sleep(1000);//确保全部线程都执行完毕
System.out.println("列表大小:"+list.size());
System.out.println("队列大小:"+queue.size());
System.out.println("阻塞队列大小:"+block.size());
System.out.println("end");
System.out.println("========================================================");
}
}
public static void demo(){
for (int i = 0; i < 10000; i++) {
Thread aa = new Thread(new Runnable() {
public void run() {
list.add("AA");
}
});
aa.start();
}
for (int i = 0; i < 10000; i++) {
Thread aa = new Thread(new Runnable() {
public void run() {
queue.add("aa");
}
});
aa.start();
}
for (int i = 0; i < 10000; i++) {
Thread aa = new Thread(new Runnable() {
public void run() {
try {
block.put("s");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
aa.start();
}
}
}