0% found this document useful (0 votes)
2 views7 pages

Understanding ConcurrentModificationException

The document discusses the ConcurrentModificationException in Java, illustrating how it occurs when modifying a collection while iterating over it. It also covers string interning and reference comparison in Java, as well as a deadlock example involving two threads trying to acquire two resources. Additionally, it mentions the use of ReentrantLock and tryLock for managing concurrency.

Uploaded by

rahul11797
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views7 pages

Understanding ConcurrentModificationException

The document discusses the ConcurrentModificationException in Java, illustrating how it occurs when modifying a collection while iterating over it. It also covers string interning and reference comparison in Java, as well as a deadlock example involving two threads trying to acquire two resources. Additionally, it mentions the use of ReentrantLock and tryLock for managing concurrency.

Uploaded by

rahul11797
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

ConcurrentModificationException

ConcurrentModificationException


[Link] ArrayList HashMap HashSet
java CopyEdit import [Link]; public class Main { public
static void main(String[] args) { ArrayList<Integer> list = new Arra
yList<>(); [Link](1); [Link](2); [Link](3); for (Integer num :
list) { if (num == 2) { [Link]([Link](2)); // Structur
al modification } } } }

ConcurrentModificationException


[Link] CopyOnWriteArrayList CopyOn
WriteArraySet ConcurrentHashMap

import [Link]; public class Main


{ public static void main(String[] args) { CopyOnWriteArrayList<Inte
ger> list = new CopyOnWriteArrayList<>(); [Link](1); [Link](2);
[Link](3); for (Integer num : list) { if (num == 2) { [Link](4);
// Structural modification } } [Link](list); } }
[1, 2, 3, 4]

ConcurrentModificationE
xception

ArrayList HashMap HashSet CopyOnWriteArrayList Concurr


entHashMap

• String str1 = "Hello";


"Hello"

• String str2 = "Hello";


"Hello"

new

• new String str3 = new


String("Hello");


intern() String str4 = [Link]();

== .equals()

• ==
true

String s1 = "Java"; String s2 = "Java"; [Link](s1


== s2); // true, as both refer to the same pool object

• .equals() true

public class Main { public static void main(String[] args) { String


str1 = "Hello"; String str2 = "Hello"; String str3 = new String("Hel
lo"); // Checking references [Link](str1 == str2); // tr
ue (both refer to the String Pool) [Link](str1 == str3);
// false (str3 is in the heap) // Using intern() String str4 = str3.
intern(); [Link](str1 == str4); // true (intern() adds s
tr3 to the pool) } }
Thread-1 Thread-2 Resource-
A Resource-B

Resource-A Resource-B

Resource-B Resource-A

public class DeadlockExample { private static final Object resourceA


= new Object(); private static final Object resourceB = new Object
(); public static void main(String[] args) { Thread thread1 = new Th
read(() -> { synchronized (resourceA) { [Link]("Thread-
1: Locked Resource-A"); try { [Link](100); // Simulate some wo
rk } catch (InterruptedException e) { [Link](); } System.
[Link]("Thread-1: Waiting for Resource-B"); synchronized (resou
rceB) { [Link]("Thread-1: Locked Resource-B"); } } }); T
hread thread2 = new Thread(() -> { synchronized (resourceB) { Syste
[Link]("Thread-2: Locked Resource-B"); try { [Link](10
0); // Simulate some work } catch (InterruptedException e) { [Link]
StackTrace(); } [Link]("Thread-2: Waiting for Resource-
A"); synchronized (resourceA) { [Link]("Thread-2: Locked
Resource-A"); } } }); [Link](); [Link](); } }
mathematica CopyEdit Thread-1: Locked Resource-A Thread-2: Locked Re
source-B Thread-1: Waiting for Resource-B Thread-2: Waiting for Reso
urce-A

Thread-1 Thread-2


• Resource-A Resource-B

• ReentrantLock tryLock()

if ([Link](1000, [Link])) { // Do work } el


se { // Handle failure to acquire lock }

• finally

You might also like