如何证明ArrayList是不是线程安全与测试? [英] How to prove arraylist is not thread safe with a test?

查看:400
本文介绍了如何证明ArrayList是不是线程安全与测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的应用程序,我们上了 ArrayList.add(对象o)的ArrayIndexOutOfBounds一个例外操作。最明显的解释是线程安全的,但我没能重现的事件。我试图创建两个线程。在一个我添加元素,在其他的,我删除它们(或清除数组),但我没有得到第二次例外。
我的意思是很明显它可以通过查看ArrayList的源发生,但它会是不错的能够证明它。

我一直在运行这个测试相当长的一段时间没有任何异常:

 公共类的测试{
静态ArrayList的一个=新的ArrayList();公共静态无效的主要(字串[] args)抛出异常{
    螺纹T1 =新的Thread(){
        公共无效的run(){
            而(真){
                如果(a.size()大于0)
                    a.remove(0);
            }
        }
    };    螺纹T2 =新的Thread(){
        公共无效的run(){
            而(真){
                a.add(新对象());
            }
        }
    };    t2.start();
    视频下载(100);
    t1.start();
}
}


解决方案

感谢来自isnot2bad发表评论我在我的假设发现了一个问题。
问题是并发补充说,不添加/删除。
我能够创建一个失败的测试:

 静态ArrayList的一个=新的ArrayList(1);公共静态无效的主要(字串[] args)抛出异常{
    螺纹T1 =新的Thread(){
        公共无效的run(){
            而(真){
                a.add(新对象());
            }
        }
    };    螺纹T2 =新的Thread(){
        公共无效的run(){
            而(真){
                一个=新的ArrayList(1);
                a.add(新对象());
                a.add(新对象());
            }
        }
    };    t2.start();
    视频下载(100);
    t1.start();
}

在与第一线的添加就行,我得到这样的:

 异常螺纹线程0java.lang.ArrayIndexOutOfBoundsException:2

:)

In our application we got an ArrayIndexOutOfBounds exception on the ArrayList.add(Object o) operation. The most obvious explanation is thread safety, but I wasn't able to recreate the events. I've tried creating two threads. In one I'm adding elements, in the other I'm removing them(or clearing the array), but I didn't get the exception for the second time. I mean it's obvious it can happen by looking at the source of ArrayList, but it would be nice to be able to demonstrate it.

I've been running this test for quite some time without any exception:

public class Test {
static ArrayList a = new ArrayList();

public static void main(String[] args) throws Exception {
    Thread t1 = new Thread() {
        public void run() {
            while (true) {
                if (a.size() > 0)
                    a.remove(0);
            }
        }
    };

    Thread t2 = new Thread() {
        public void run() {
            while (true) {
                a.add(new Object());
            }
        }
    };

    t2.start();
    Thread.sleep(100);
    t1.start();
}
}

解决方案

Thanks to comment from isnot2bad I found a problem in my assumptions. The problem is with concurrent adds, not add/remove. I was able to create a failing test:

static ArrayList a = new ArrayList(1);

public static void main(String[] args) throws Exception {
    Thread t1 = new Thread() {
        public void run() {
            while (true) {
                a.add(new Object());
            }
        }
    };

    Thread t2 = new Thread() {
        public void run() {
            while (true) {
                a = new ArrayList(1);
                a.add(new Object());
                a.add(new Object());
            }
        }
    };

    t2.start();
    Thread.sleep(100);
    t1.start();
}

On the line with the add at the first thread, I'm getting this:

Exception in thread "Thread-0" java.lang.ArrayIndexOutOfBoundsException: 2 

:)

这篇关于如何证明ArrayList是不是线程安全与测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆