Are you taking CLEP classes? Try clep.ai
| preferred AP College board partner for AP classes
AP Computer Science A/Unit 7: ArrayList
Start Practice TestPractice Test
About Exam
hard Solved by 2 students

ArrayList Thread Synchronization Issue

Mark
< Prev
Next >

In the following code, a writer thread synchronizes its additions to a shared ArrayList. What concurrency issue may still occur due to the reader thread’s unsynchronized iteration?

import java.util.ArrayList;

public class SynchronizedIterationTest {
    static ArrayList<Integer> list = new ArrayList<>();
    public static void main(String[] args) {
        Thread writer = new Thread(() -> {
            synchronized(list) {
                for (int i = 0; i < 50; i++) {
                    list.add(i);
                    try { Thread.sleep(10); } catch (InterruptedException e) {}
                }
            }
        });
        Thread reader = new Thread(() -> {
            for (Integer num : list) {
                System.out.println(num);
                try { Thread.sleep(15); } catch (InterruptedException e) {}
            }
        });
        writer.start();
        reader.start();
    }
}
A

Deadlock from nested synchronization

B

NullPointerException from accessing invalid indices

C

ConcurrentModificationException due to unsynchronized iteration

D

Race condition causing duplicate insertions

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

Question Leaderboard

Rank
User
Correct Count
Attempt Count
Total Time
Score
#1gopangjan89311 0m 06s 94
#2jebaksar00011 0m 48s 52
#3lightingstrikes134202 0m 00s -20
Items per page:
10
1 – 3 of 3

AI Tutor

How can I help?

APFIVE © 2020.
Email: apfive@apfive.org|Privacy Policy|