Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8905075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:18:01+00:00 2026-06-15T02:18:01+00:00

My Previous question brought me to this question. Is add function of ArrayList thread

  • 0

My 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T02:18:02+00:00Added an answer on June 15, 2026 at 2:18 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is brought on by a previous question . Apparently ImageList doesn't support animated
This question is extended part of my previous question, Finding number position in string
This question is based on my previous question which I got a working answer
This question is related to a previous question of mine That's my current code
This question follows on from a previous question, that has raised a further issue.
This question is related to my previous question How to generate Cartesian Coordinate (x,y)
This question is related to my previous question . The storyline: I have an
This question is a continuation of my previous question here zend models architecture (big
This question is in continuation to my previous question . I am trying to
This question is related to my previous question - MySQL query to show records

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.