1

There are two threads A and B. A keeps writing to an ArrayList and B keeps reading from it. The following code is for reading the ArrayList and belongs to thread B.

The issue with this code is that sometimes both of the IF statements becomes true which should not be possible. First IF is there to ensure that list is not read beyond its size. Second IF is not a part of actual code. I have put it just for verification purposes. As you can see in the Output below, size of the list is greater than writeNext variable, I don't understand why this returns null. And yes, if I place a print statement or a a very small delay just after first IF, everything works fine. Please remember that there is just one thread that writes into the ArrayList, so issue should not be related to synchronization.

 public void run() {// thread B

public class WriteTicksToFile implements Runnable, Serializable {

    int writeNext = 0;

    @Override
    public void run() {

        while (true) {

            // read till writeNext is equal to ArrayList.size()-1 i.e. the last index
            if (writeNext < TickData.getCompleteTickList().size()) {

                // System.out.println(TickData.getCompleteTickList().size()+" "+writeNext);
                if (TickData.getCompleteTickList().get(writeNext) == null) {

                    System.out.println("ListSize: " + TickData.getCompleteTickList().size() + " " + "Read@Index: " + writeNext);

                }// if ends

                // write element to file.
                writeTick(TickData.getCompleteTickList().get(writeNext));
                writeNext++;
            }// if ends
        }// while ends
    }
}

Output:

Number of symbols found: 19
Market Opening time: 81500
Market Closing time: 235900
Current Time: 203931
Waiting for market to open...
Market Opened @ 203931
ListSize: 15876 Read@Index: 15875
ListSize: 15877 Read@Index: 15876
13
  • If there is a separate thread writing into the list, then it might just write into it right while the reading thread is between the two if statements. So I would guess it actually is related to synchronization afterall. Commented Jun 22, 2014 at 20:52
  • @IngoBürk Agreed; I don't see any mechanism for ensuring exclusive access to the list. Commented Jun 22, 2014 at 20:53
  • Show us the code used by Thread A. Commented Jun 22, 2014 at 20:56
  • Possibly, the size of the list is incremented, before the element is available through the get() method. Whenever there are two threads working on one object, you should ensure proper synchronization. Commented Jun 22, 2014 at 20:58
  • 1
    Take a look at stackoverflow.com/questions/11079210/…. There are a number of fast solutions to this problem. Commented Jun 22, 2014 at 20:59

1 Answer 1

4

Please remember that there is just one thread that writes into the ArrayList, so issue should not be related to synchronization.

That is simply not true according to the javadoc for ArrayList:

Note that this implementation is not synchronized. If multiple threads access an ArrayList instance concurrently, and at least one of the threads modifies the list structurally, it must be synchronized externally.

You have multiple threads accessing an ArrayList concurrently and one of them is modifying the list structurally.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.