3.1 Lesson Outcomes
After completing this lesson, learners will be able to:
- Apply synchronized methods in Java.
- Use ReentrantLock for thread control.
- Implement ConcurrentMap collections.
- Apply AtomicInteger operations.
- Use concurrent collections safely in multithreaded applications.
3.2 Overview
Concurrency allows multiple processes or threads to execute simultaneously within a Java application. Java provides several concurrency tools used to manage shared resources safely and efficiently.
Concurrency concepts are important in:
- e-commerce systems,
- banking applications,
- enterprise systems,
- inventory management,
- and large-scale software platforms.
This lesson introduces learners to practical concurrency techniques using Java programming.
Understanding concurrency is important because modern applications process multiple operations at the same time.
PA0401 — Getting Started with synchronized
The synchronized keyword controls access to shared resources.
Java Example:
class Counter {
private int count = 0;
public synchronized void increment() {
count++;
}
public int getCount() {
return count;
}
}
public class SyncDemo {
public static void main(String[] args) throws InterruptedException {
Counter counter = new Counter();
Thread t1 = new Thread(() -> {
for (int i = 0; i < 1000; i++)
counter.increment();
});
Thread t2 = new Thread(() -> {
for (int i = 0; i < 1000; i++)
counter.increment();
});
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println(“Final count: “ + counter.getCount());
}
}
Practical Activity
Learners must:
- create synchronized methods,
- manage shared resources,
- and display thread-safe outputs.
PA0402 — Enter Locks with ReentrantLock
ReentrantLock provides advanced locking mechanisms.
Java Example:
import java.util.concurrent.locks.ReentrantLock;
class SharedResource {
private int value = 0;
private final ReentrantLock lock = new ReentrantLock();
public void increment() {
lock.lock();
try {
value++;
} finally {
lock.unlock();
}
}
public int getValue() {
return value;
}
}
Practical Activity
Learners must:
- create lock-based programs,
- apply ReentrantLock,
- and control concurrent access.
PA0403 — Use ConcurrentMap
ConcurrentMap supports thread-safe key-value operations.
Java Example:
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
public class ConcurrentMapDemo {
public static void main(String[] args) {
ConcurrentMap<String, Integer> stock = new ConcurrentHashMap<>();
stock.put(“Shoes”, 50);
stock.putIfAbsent(“Bags”, 20);
stock.computeIfPresent(“Shoes”, (k, v) -> v – 1);
System.out.println(stock);
}
}
Practical Activity
Learners must:
- create ConcurrentMaps,
- store thread-safe data,
- and update shared collections.
PA0404 — Apply AtomicInteger
AtomicInteger performs thread-safe numeric operations.
Java Example:
import java.util.concurrent.atomic.AtomicInteger;
public class AtomicDemo {
public static void main(String[] args) {
AtomicInteger ai = new AtomicInteger(0);
ai.incrementAndGet();
ai.addAndGet(5);
System.out.println(“Atomic Value: “ + ai);
}
}
Practical Activity
Learners must:
- create AtomicInteger objects,
- apply atomic operations,
- and display thread-safe values.
PA0405 — Implement ConcurrentHashMap for Different Regions
ConcurrentHashMap supports concurrent updates efficiently.
Java Example:
import java.util.concurrent.ConcurrentHashMap;
public class RegionSales {
public static void main(String[] args) {
ConcurrentHashMap<String, Integer> sales = new ConcurrentHashMap<>();
sales.put(“Gauteng”, 120);
sales.put(“KZN”, 80);
sales.put(“Western Cape”, 95);
sales.compute(“Gauteng”, (k, v) -> v + 50);
sales.compute(“KZN”, (k, v) -> v + 30);
System.out.println(sales);
}
}
Practical Activity
Learners must:
- create ConcurrentHashMaps,
- process concurrent updates,
- and display regional data safely.
PA0406 — Use CopyOnWrite Concurrent Collections
CopyOnWrite collections support safe iteration during modifications.
Java Example:
import java.util.concurrent.CopyOnWriteArrayList;
public class CopyOnWriteDemo {
public static void main(String[] args) {
CopyOnWriteArrayList customers = new CopyOnWriteArrayList<>();
customers.add(“Alice”);
customers.add(“Bob”);
for (String c : customers) {
System.out.println(c);
customers.add(“Charlie”);
}
System.out.println(customers);
}
}
Practical Activity
Learners must:
- create CopyOnWrite collections,
- modify collections during iteration,
- and display concurrent outputs safely.
AK0401 — Functions and Uses of Concurrent Collections and Atomic Operations
Concurrency Feature — Function
synchronized — Restricts thread access
ReentrantLock — Advanced thread locking
ConcurrentMap — Thread-safe map operations
AtomicInteger — Atomic numeric operations
CopyOnWriteArrayList — Safe concurrent iteration
Practical Activity
Learners must:
- identify concurrency tools,
- explain thread safety,
- and apply concurrent programming techniques.
IAC0401 — Expected Results with Concurrency and Concurrent Collections and Atomic Operations in Java are Achieved
Expected Results
- synchronized methods function correctly.
- ReentrantLock controls shared resources safely.
- ConcurrentMap manages concurrent data correctly.
- AtomicInteger performs atomic operations successfully.
- Concurrent collections support thread-safe execution.
3.5 Key Notes / Summary
- Concurrency allows simultaneous task execution.
- synchronized controls shared resource access.
- ReentrantLock provides advanced locking functionality.
- ConcurrentMap supports thread-safe collections.
- AtomicInteger performs lock-free operations.
- CopyOnWrite collections support safe iteration.
- Concurrency improves application performance and scalability.