开发者

ArrayList ThreadSafety Test Code fails

Given, that add method defination in ArrayList is as follows :-

public boolean add(E e) {
ensureCapacity(size + 1);  // Increments modCount!!
elementData[size++] = e;
return true;
}

Please find following program to check Thread safety of ArrayList.

package pack4;

import java.util.ArrayList;

public class Demo {

    public static void main(String[] args) {
        ArrayList<String> al = new ArrayList<String>() ;
        new AddFirstElementThread(al).start() ;
        new RemoveFirstElementThread(al).start() ;
    }
}

class AddFirstElementThread extends Thread{

    ArrayList<String> list ;

    public AddFirstElementThread(ArrayList<String> l) {
        list = l ;
    }

    @Override
    public void run() {
        while(true){
            if(list.size() == 0){
                list.add("First element") ;
            }
        }
    }
}

class RemoveFirstElementThread extends Thread{

    ArrayList<String> list ;

    public RemoveFi开发者_开发技巧rstElementThread(ArrayList<String> l) {
        list = l ;
    }

    @Override
    public void run() {
        while(true){
            if(list.isEmpty()){
                try{
                    list.get(0) ;
                    System.out.println("Hence Proved, that ArrayList is not Thread-safe.");
                    System.exit(1) ;
                }catch (Exception e) {
                        //continue, if no value is there at index 0
                }
            }
        }
    }
}

But, the program never terminates, thus fails to prove thread-safety of ArrayList.

Please, suggest correct implementation to test Thread-safe behaviour of ArrayList and Vector.

Thanks & Best Regards,

Rits


ArrayList is not thread-safe; Vector is. You can wrap an ArrayList with Collections.synchronizedList() if you require it.


The point about unsafe code is there is no guarantee how it will behave when multiple thread are used. You cannot guarantee unsafe code will fail. This is because code is not written to be unsafe, it may not have any guarantees that it is. Thread safety can only be determined by reading and understanding the code.

The problem with thread safety is that it is very hard to prove experimentally. Its not easy to prove something is not thread safe unless you know the exact edge case which will trigger an issue. Additionally, thread safety issues are more or less likely to show depending on the architecture of your system and the load on it. i.e. it can work fine for days and fail unpredictably.


"Thread-safety" on Collections was a pretty bad idea to start with for the vast majority of cases, because it's way too narrow and you need some higher level synchronization anyhow.

In case you really want to remove the first element or add an element to a list, you are better off with for example this here, but your contrived example may need some higher synchronization anyhow (depends on what exactly the semantics should be - if you don't see why, it's probably a really good idea to read something about concurrency)

And finally just have a look at the concurrent framework.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜