| preferred AP College board partner for AP classes
AP Computer Science A/Unit 5: Writing Classes
Start Practice TestPractice Test
About Exam
hard
Concurrent Modification of an ArrayList
< Prev
Next >

When multiple threads run ListUpdater concurrently, what is the most likely issue that may occur with the ArrayList?

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

public class ListUpdater implements Runnable {
    private List<Integer> list;
    
    public ListUpdater(List<Integer> list) {
        this.list = list;
    }
    
    public void run() {
        for (int i = 0; i < 1000; i++) {
            list.add(i);
        }
    }
}

public class ListTest {
    public static void main(String[] args) throws InterruptedException {
        List<Integer> myList = new ArrayList<>();
        Thread t1 = new Thread(new ListUpdater(myList));
        Thread t2 = new Thread(new ListUpdater(myList));
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println(myList.size());
    }
}
A

Race condition resulting in data corruption due to the non-thread-safe nature of ArrayList.

B

OutOfMemoryError because ArrayList cannot handle multiple threads.

C

Deadlock caused by the inherent locking mechanism in ArrayList.

D

InterruptedException being thrown due to concurrent modifications.

Hint
Did You Know?
Explain Why
Explain All Answers
Check Answer
Show Correct Answer
Report Question

Question Leaderboard

Not enough data yet to show leaderboard.

No comments yet. Be the first to comment!

AI Tutor

How can I help?

APFIVE © 2020.
Email: [email protected]|Privacy Policy