Discussion
Sample43 is run - what will be the output?
import java.util.*;
public class Sample43
{
static final Map<Integer, Integer> MAP = new HashMap<>();
public static void main(String[] args) throws InterruptedException
{
Runnable job = () -> { for (int i = 0; i < 1000; i++) MAP.put(i, i); };
Thread a = new Thread(job), b = new Thread(job);
a.start(); b.start(); a.join(); b.join();
System.out.println(MAP.size());
}
}
Answer: C
Both threads write the same 1000 keys, so a correct result would be a size of 1000, but HashMap is unsynchronized and concurrent structural modification can leave the table in an inconsistent state. The size may be wrong and entries may be lost. ConcurrentModificationException guards iteration, not concurrent puts.
No comment is present. Be the first to comment.
Loading…
Post your comment
Please log in or register to post a comment.
Examination Categories
- SSC Exams
- Banking Exams
- Teaching Exams
- Civil Services Exams
- Railways Exams
- Engineering Recruitment Exams
- Defence Exams
- State Government Exams
- Police Exams
- Insurance Exams
- Nursing Exams
- Other Government Exams
- NRA CET
- PG Entrance Exams
- Campus Placement
- MBA Entrance Exams
- CUET and UG Entrance
- Judiciary Exams
- Regulatory Body Exams
- Software Development