ArrayList.add(int index,E element)线程不安全吗? [英] Is ArrayList.add(int index, E element) thread unsafe?

查看:107
本文介绍了ArrayList.add(int index,E element)线程不安全吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的上一个问题使我想到了这个问题.

ArrayList线程的添加功能安全吗?

我用以下课程制作了一个示例应用程序

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


public class ThreadTest
{
    public static List<DummyObject> list = null;
    public static boolean isLoaded = false;
   public static void main(String [] args)
   {
      MyThread t1 = new MyThread(1);
      MyThread t2 = new MyThread(2);

      t1.start();
      t2.start();
   }

   public static void loadObject(){
       if(isLoaded){
           return;
       }
       isLoaded = false;
       try{
       list = new ArrayList<DummyObject>();
       for(int i=0;i<10;i++){
           list.add(i,new DummyObject());
       }}
       catch(Exception e){
           e.printStackTrace();
       }
       isLoaded = true;
   }
}

这些是我的话题

public class MyThread extends Thread
{
   int threadNumber ;
   public MyThread(int threadNumber)
   {
      this.threadNumber = threadNumber;
   }

   @Override
   public void run()
   {
       try {
        sleep(10-threadNumber);
    } catch (InterruptedException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
     System.out.println("Running Thread: " + threadNumber);
     ThreadTest.loadObject();
     if(ThreadTest.isLoaded){
         System.out.println(ThreadTest.list);
         for(int i=0;i<ThreadTest.list.size();i++){
             if(ThreadTest.list.get(i)==null){
                 throw new NullPointerException();
             } 
         }
     }else {
         try {
                sleep(10);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
         }
   }
}

这是我的虚拟课

public class DummyObject {

}

即使我无法复制在

Form ArrayList代码这是引发错误的行:

if (index > size || index < 0)
        throw new IndexOutOfBoundsException(
        "Index: "+index+", Size: "+size);

但是从异常索引为1且大小为10 可以看出,所以如果条件满足,则是不可能的.那么我的假设正确吗,即arrayList的add函数是线程不安全的,还是这里发生了其他事情?

来自文档:

(该类与Vector大致等效,但它是不同步的.)

您需要自己实现同步,或者更好地使用同步容器,例如Previous question brought me to this question.

Is add function of ArrayList thread safe?

I made a sample application with following classes

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


public class ThreadTest
{
    public static List<DummyObject> list = null;
    public static boolean isLoaded = false;
   public static void main(String [] args)
   {
      MyThread t1 = new MyThread(1);
      MyThread t2 = new MyThread(2);

      t1.start();
      t2.start();
   }

   public static void loadObject(){
       if(isLoaded){
           return;
       }
       isLoaded = false;
       try{
       list = new ArrayList<DummyObject>();
       for(int i=0;i<10;i++){
           list.add(i,new DummyObject());
       }}
       catch(Exception e){
           e.printStackTrace();
       }
       isLoaded = true;
   }
}

These are my threads

public class MyThread extends Thread
{
   int threadNumber ;
   public MyThread(int threadNumber)
   {
      this.threadNumber = threadNumber;
   }

   @Override
   public void run()
   {
       try {
        sleep(10-threadNumber);
    } catch (InterruptedException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
     System.out.println("Running Thread: " + threadNumber);
     ThreadTest.loadObject();
     if(ThreadTest.isLoaded){
         System.out.println(ThreadTest.list);
         for(int i=0;i<ThreadTest.list.size();i++){
             if(ThreadTest.list.get(i)==null){
                 throw new NullPointerException();
             } 
         }
     }else {
         try {
                sleep(10);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
         }
   }
}

This is my dummy class

public class DummyObject {

}

Even though I wasn't able to replicate the Null Pointer Exception that I got on my previous question , I sometimes get this error

Exception in thread "Thread-1" java.lang.IndexOutOfBoundsException: Index: 1, Size: 10
    at java.util.ArrayList.add(ArrayList.java:367)
    at ThreadTest.loadObject(ThreadTest.java:25)
    at MyThread.run(MyThread.java:20)

Form ArrayList Code this is the line thats throwing an error:

if (index > size || index < 0)
        throw new IndexOutOfBoundsException(
        "Index: "+index+", Size: "+size);

But as we can see from Exception index is 1 and size is 10, so there is no way that if condition is satisfied. So is my assumption correct that add function of arrayList is thread unsafe or is something else going on here?

解决方案

From the documentation:

(This class is roughly equivalent to Vector, except that it is unsynchronized.)

You need to either implement the synchronization yourself, or better yet, use a synchronized container like Vector.

In the case of your code, you have 2 threads running the same piece of code (loadObject) in which several static values are accessed/modified. You need to make sure that each access is done in a synchronized manner. You have 2 threads, thus you allocate twice the ThreadTest.list field, so one of the allocation is useless, but more importantly, there might be some values inserted in that list before it is lost, so these values become lost as well.

You should make sure that the list is not allocated before allocating it.

You could also have problem with the isLoaded field, leading to more than 10 elements in your list.

这篇关于ArrayList.add(int index,E element)线程不安全吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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