线程安全性

4 篇文章 0 订阅
2 篇文章 0 订阅

    锁是多线程开发的必要工具之一,它的基本作用是保护临界区资源不会被多个线程同时访问而受到破坏。通过锁,可以让多个线程排队进入临界区访问目标对象,使目标对象的状态总是保持一致。 

一。 线程安全性    

先用一个案例演示在多线程情况,对ArrayList的并发问题. 

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

public class Test1 {
    public static List<Integer> numberList=new ArrayList<Integer>();
    public static class AddToList implements Runnable{
        int startnum=0;
        public AddToList( int startnum){
            this.startnum=startnum;
        }
        @Override
        public void run() {
            int count=0;
            while( count<1000000){
                numberList.add( startnum);
                startnum+=2;
                count++;
            }
        }
    }
    public static void main(String[] args) {
        Thread t1=new Thread(   new AddToList(0) );
        Thread t2=new Thread(  new AddToList(1));
        t1.start();
        t2.start();
    }
}

程序运行后,抛出以下错误:

Exception in thread "Thread-1" java.lang.ArrayIndexOutOfBoundsException: 10
	at java.util.ArrayList.add(ArrayList.java:459)
	at com.yc.Test1$AddToList.run(Test1.java:17)
	at java.lang.Thread.run(Thread.java:745)

追踪ArrayList的源码:

  public boolean add(E e) {
        ensureCapacityInternal(size + 1);  // Increments modCount!!
        elementData[size++] = e;        //索引下标越界
        return true;
    }

出现这个问题是因为两个线程同时对ArrayList进行写操作,破坏了ArrayList内部数据的一致性,导致其中一个线程访问了错误的数组索引。

解决方案:使用Vector代替ArrayList,在Vector的实现中,使用了内部锁对List对象进行控制. 

 public synchronized boolean add(E e) {
        modCount++;
        ensureCapacityHelper(elementCount + 1);
        elementData[elementCount++] = e;
        return true;
    }

关键字synchronized保证每次只有一个线程可以访问对象实例,确保了多线程环境中对对象内部数据的一致性. 

那么synchronized是如何实现的呢,这就与锁机制有关系了,后面我将一一讲解. 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhangyingchengqi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值