证明ArrayList是线程不安全的


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();
		}
	}
}

修改后的方案

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值