锁是多线程开发的必要工具之一,它的基本作用是保护临界区资源不会被多个线程同时访问而受到破坏。通过锁,可以让多个线程排队进入临界区访问目标对象,使目标对象的状态总是保持一致。
一。 线程安全性
先用一个案例演示在多线程情况,对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是如何实现的呢,这就与锁机制有关系了,后面我将一一讲解.