为什么说ArrayList是线程不安全的?

本文通过实例演示了ArrayList在多线程环境下可能出现的数据不一致性问题,对比了ArrayList与线程安全的Vector及CopyOnWriteArrayList的区别,并解释了ArrayList为何线程不安全。

为什么说ArrayList是线程不安全的?

个人记录:2018年,工作的第2到3个年头。
重点研究自己不太擅长的技术:分布式、高并发、大数据量、数据库优化、高性能、负载均衡等。
刷题是一种态度,是一种好习惯。


把原来的例子,运行了下,增加了CopyOnWriteArrayList。

package cn.fansunion.list;


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;


public class ArrayListInThread implements Runnable {
// 非线程安全
List<String> list1 = null;


public ArrayListInThread() {
list1 = new ArrayList<String>();
// 线程安全,synchronized (mutex)
//list1 = Collections.synchronizedList(new ArrayList<String>());
// 线程安全,ReentrantLock
list1 = new CopyOnWriteArrayList<>();
}


public void run() {
try {
Thread.sleep((int) (Math.random() * 2));
} catch (InterruptedException e) {
e.printStackTrace();
}
list1.add(Thread.currentThread().getName());
}


public static void main(String[] args) throws InterruptedException {
ThreadGroup group = new ThreadGroup("mygroup");
ArrayListInThread t = new ArrayListInThread();
for (int index = 0; index < 10000; index++) {
Thread thread = new Thread(group, t, String.valueOf(index));
thread.start();
}


while (group.activeCount() > 0) {
Thread.sleep(10);
}
System.out.println();
// 线程安全,就是1万。否则,可能不是10000。
System.out.println(t.list1.size());
}
}

 

package cn.fansunion.list; 

 

import java.util.ArrayList; 

import java.util.Collections; 

import java.util.List; 

import java.util.concurrent.CopyOnWriteArrayList; 

 

public class ArrayListInThread implements Runnable { 

    // 非线程安全 

    List<String> list1 = null

 

    public ArrayListInThread() { 

        list1 = new ArrayList<String>(); 

        // 线程安全,synchronized (mutex) 

        //list1 = Collections.synchronizedList(new ArrayList<String>()); 

        // 线程安全,ReentrantLock 

        list1 = new CopyOnWriteArrayList<>(); 

    } 

 

    public void run() { 

        try

         Thread.sleep((int) (Math.random() * 2)); 

        } catch (InterruptedException e) { 

            e.printStackTrace(); 

        } 

        list1.add(Thread.currentThread().getName()); 

    } 

 

    public static void main(String[] args) throws InterruptedException { 

        ThreadGroup group = new ThreadGroup("mygroup"); 

        ArrayListInThread t = new ArrayListInThread(); 

        for (int index = 0; index < 10000; index++) { 

            Thread thread = new Thread(group, t, String.valueOf(index)); 

           thread.start(); 

        } 

 

        while (group.activeCount() > 0) { 

            Thread.sleep(10); 

        } 

       System.out.println(); 

        // 线程安全,就是1万。否则,可能不是10000。 

        System.out.println(t.list1.size());  

    } 

package cn.fansunion.list;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

public class ArrayListInThread implements Runnable {
	// 非线程安全
	List<String> list1 = null;

	public ArrayListInThread() {
		list1 = new ArrayList<String>();
		// 线程安全,synchronized (mutex)
		//list1 = Collections.synchronizedList(new ArrayList<String>());
		// 线程安全,ReentrantLock
		list1 = new CopyOnWriteArrayList<>();
	}

	public void run() {
		try {
			Thread.sleep((int) (Math.random() * 2));
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		list1.add(Thread.currentThread().getName());
	}

	public static void main(String[] args) throws InterruptedException {
		ThreadGroup group = new ThreadGroup("mygroup");
		ArrayListInThread t = new ArrayListInThread();
		for (int index = 0; index < 10000; index++) {
			Thread thread = new Thread(group, t, String.valueOf(index));
			thread.start();
		}

		while (group.activeCount() > 0) {
			Thread.sleep(10);
		}
		System.out.println();
		// 线程安全,就是1万。否则,可能不是10000。
		System.out.println(t.list1.size()); 
	}
}

----------------------------------------------------------------------------

原文链接:http://blog.youkuaiyun.com/jiaochunyu1992/article/details/51177373

概要介绍

首先说一下什么是线程不安全:线程安全就是多线程访问时,采用了加锁机制,当一个线程访问该类的某个数据时,进行保护,其他线程不能进行访问直到该线程读取完,其他线程才可使用。不会出现数据不一致或者数据污染。线程不安全就是不提供数据访问保护,有可能出现多个线程先后更改数据造成所得到的数据是脏数据。 如图,List接口下面有两个实现,一个是ArrayList,另外一个是vector。 从源码的角度来看,因为Vector的方法前加了,synchronized 关键字,也就是同步的意思,sun公司希望Vector是线程安全的,而希望arraylist是高效的,缺点就是另外的优点。 说下原理(百度的,很好理解): 一个 ArrayList ,在添加一个元素的时候,它可能会有两步来完成:
1. 在 Items[Size] 的位置存放此元素;
2. 增大 Size 的值。
在单线程运行的情况下,如果 Size = 0,添加一个元素后,此元素在位置 0,而且 Size=1;
而如果是在多线程情况下,比如有两个线程,线程 A 先将元素存放在位置 0。但是此时 CPU 调度线程A暂停,线程 B 得到运行的机会。线程B也向此 ArrayList 添加元素,因为此时 Size 仍然等于 0 (注意哦,我们假设的是添加一个元素是要两个步骤哦,而线程A仅仅完成了步骤1),所以线程B也将元素存放在位置0。然后线程A和线程B都继续运行,都增加 Size 的值。
那好,现在我们来看看 ArrayList 的情况,元素实际上只有一个,存放在位置 0,而 Size 却等于 2。这就是“线程不安全”了。
示例程序:
 

package test; 

 

import java.util.ArrayList; 

import java.util.List; 

 

public class ArrayListInThread implements Runnable { 

    List<String> list1 = new ArrayList<String>();// not thread safe 

 

//    List<String> list1 = Collections.synchronizedList(new ArrayList<String>());// thread safe 

    public void run() { 

        try

            Thread.sleep((int)(Math.random() * 2)); 

        } 

        catch (InterruptedException e) { 

            e.printStackTrace(); 

        } 

        list1.add(Thread.currentThread().getName()); 

    } 

 

    public static void main(String[] args) throws InterruptedException { 

        ThreadGroup group = new ThreadGroup("mygroup"); 

       ArrayListInThread t = new ArrayListInThread(); 

        for (int i = 0; i < 10000; i++) { 

            Thread th = new Thread(group, t, String.valueOf(i)); 

            th.start(); 

        } 

 

        while (group.activeCount() > 0) { 

            Thread.sleep(10); 

        } 

        System.out.println(); 

        System.out.println(t.list1.size()); // it should be 10000 if thread safe collection is used. 

    } 

package test;

import java.util.ArrayList;
import java.util.List;

public class ArrayListInThread implements Runnable {
    List<String> list1 = new ArrayList<String>();// not thread safe

//    List<String> list1 = Collections.synchronizedList(new ArrayList<String>());// thread safe
    public void run() {
        try {
            Thread.sleep((int)(Math.random() * 2));
        }
        catch (InterruptedException e) {
            e.printStackTrace();
        }
        list1.add(Thread.currentThread().getName());
    }

    public static void main(String[] args) throws InterruptedException {
        ThreadGroup group = new ThreadGroup("mygroup");
        ArrayListInThread t = new ArrayListInThread();
        for (int i = 0; i < 10000; i++) {
            Thread th = new Thread(group, t, String.valueOf(i));
            th.start();
        }

        while (group.activeCount() > 0) {
            Thread.sleep(10);
        }
        System.out.println();
        System.out.println(t.list1.size()); // it should be 10000 if thread safe collection is used.
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值