AtomicInteger in Java is a simple yet powerful concept for multithreading. In Java, the operation count++ appears to be a single action, but it actually involves multiple steps: 1. Read the current value 2. Increment the value 3. Write the updated value back In a single-threaded program, this is manageable. However, in a multi-threaded environment, multiple threads can read the same value simultaneously, leading to overwritten updates. For example: int count = 0; count++; This approach is not thread-safe. To address this, Java offers AtomicInteger: AtomicInteger count = new AtomicInteger(0); count.incrementAndGet(); AtomicInteger supports atomic operations such as: - get() - set() - incrementAndGet() - getAndIncrement() - decrementAndGet() - addAndGet() - compareAndSet() The core concept behind AtomicInteger is CAS (Compare And Swap). CAS checks if the current value matches the expected value. If it does, the value is updated; if not, it retries or fails. AtomicInteger is particularly useful for: - Thread-safe counters - Request counters - Retry counters - Sequence generators - Simple shared numeric state For instance: AtomicInteger requestCount = new AtomicInteger(0); public void handleRequest() { int current = requestCount.incrementAndGet(); System.out.println("Request number: " + current); } It's important to note that while AtomicInteger is excellent for simple atomic operations, it may not ensure safety for complex logic that involves multiple dependent steps, such as: if (count.get() > 0) { count.decrementAndGet(); } In such cases, consider using: - synchronized - Lock - CAS loop - Other concurrency utilities In summary, AtomicInteger provides a thread-safe integer without explicit synchronization, making it one of the most valuable classes to understand when learning Java concurrency. I’m also actively strengthening my backend engineering, open-source, and problem-solving skills through GitHub and LeetCode. GitHub: https://lnkd.in/gw6_qdD4 LeetCode: https://lnkd.in/g2CWWq8n #Java #SpringBoot #Microservices #AWS #Kafka #OpenSource #LeetCode #DSA #SoftwareEngineering
Java AtomicInteger for Thread-Safe Counters and More
More Relevant Posts
-
Deadlock is one of the most important concurrency problems in Java. It happens when two or more threads are blocked forever, each waiting for a resource held by another thread. In simple words: *Thread A is waiting for Thread B *Thread B is waiting for Thread A *Neither can move *Application gets stuck forever This is called Deadlock. What is Deadlock in Java? A deadlock occurs when multiple threads hold locks that the others need, and none of them can proceed. Java deadlocks usually happen when working with: * synchronized * multiple shared resources * improper lock ordering This is a classic multithreading problem. Real-Life Analogy Imagine: * Person A has Key 1 and needs Key 2 * Person B has Key 2 and needs Key 1 Now both are waiting for each other forever. That is exactly how deadlock works in Java. Deadlock Example in Java Code Example public class DeadlockExample { private static final Object lock1 = new Object(); private static final Object lock2 = new Object(); public static void main(String[] args) { Thread thread1 = new Thread(() -> { synchronized (lock1) { System.out.println("Thread 1: Holding lock1..."); try { Thread.sleep(100); } catch (InterruptedException e) {} System.out.println("Thread 1: Waiting for lock2..."); synchronized (lock2) { System.out.println("Thread 1: Holding lock1 & lock2"); } } }); Thread thread2 = new Thread(() -> { synchronized (lock2) { System.out.println("Thread 2: Holding lock2..."); try { Thread.sleep(100); } catch (InterruptedException e) {} System.out.println("Thread 2: Waiting for lock1..."); synchronized (lock1) { System.out.println("Thread 2: Holding lock2 & lock1"); } } }); thread1.start(); thread2.start(); } } What Happens Here? Thread 1 Flow * Acquires lock1 * Waits for lock2 Thread 2 Flow * Acquires lock2 * Waits for lock1 Now: * Thread 1 cannot proceed until Thread 2 releases lock2 * Thread 2 cannot proceed until Thread 1 releases lock1 Both threads wait forever. This is a deadlock.
To view or add a comment, sign in
-
Multimap in Java:- In Java, a Multimap is a collection that maps a single key to multiple values. Unlike a standard java.util.Map, which associates each key with exactly one value, a Multimap allows you to store a "one-to-many" relationship efficiently. It is important to note that Multimap is not part of the standard Java Development Kit (JDK). It is primarily provided by popular third-party libraries like Google Guava and Apache Commons Collections. 1. Conceptual Difference Guava's Multimap is the industry standard. It provides two main sub-interfaces: ListMultimap: Uses a List to store values. It allows duplicate values for the same key and maintains insertion order. Implementation: ArrayListMultimap, LinkedListMultimap. SetMultimap: Uses a Set to store values. It does not allow duplicate values for the same key. Implementation: HashMultimap, TreeMultimap. Example using Guava: ListMultimap<String, String> multimap = ArrayListMultimap.create(); multimap.put("Fruit", "Apple"); multimap.put("Fruit", "Banana"); multimap.put("Vegetable", "Carrot"); // Returns [Apple, Banana] List<String> fruits = multimap.get("Fruit"); // Returns 3 (total entries) int totalEntries = multimap.size(); B. Apache Commons Collections Apache uses the MultiValuedMap interface. Implementation: ArrayListValuedHashMap, HashSetValuedHashMap. 3. The "Native" Java Way (Java 8+) If you don't want to add a third-party library, you can achieve Multimap behavior using a standard Map<K, Collection<V>> combined with the computeIfAbsent method introduced in Java 8. Standard JDK Implementation: Map<String, List<String>> manualMultimap = new HashMap<>(); // Adds "Apple" to the list under "Fruit", creating the list if it doesn't exist manualMultimap.computeIfAbsent("Fruit", k -> new ArrayList<>()).add("Apple"); manualMultimap.computeIfAbsent("Fruit", k -> new ArrayList<>()).add("Banana"); 4. Why use a Multimap library? While the computeIfAbsent approach works, using a dedicated library like Guava offers several advantages: Safety: get(key) returns an empty collection instead of null, avoiding NullPointerException. Cleanliness: You don't have to manually initialize collections or check if a key exists. Powerful Views: Methods like asMap() return a view of the Multimap as a Map<K, Collection<V>>, and entries() returns a flattened collection of all key-value pairs. Automatic Cleanup: If you remove the last value for a key, the key is typically removed from the map automatically. #java #javascript #python #csharp #automationtesting
To view or add a comment, sign in
-
🚀🎊Day 87 of 90 – Java Backend Development ✨🎆 In Java, a thread goes through various stages from its birth to its death. Understanding this lifecycle is crucial for managing concurrency and avoiding issues like deadlocks or resource starvation. The thread lifecycle is typically managed by the Thread Scheduler, which is part of the JVM. 👉The 5 main states of a thread: According to the Thread.State enum in Java, a thread can exist in one of the following states: 👉1. New (Born): When you create an instance of the Thread class (e.g., Thread t = new Thread();), the thread is in the New state. At this point, the code has not yet started executing, and no system resources have been allocated for it. 👉2. Runnable (Ready to Run): Once you call the .start() method, the thread moves from New to Runnable. In this state, the thread is eligible to run but is waiting for the Thread Scheduler to give it CPU time. It is essentially sitting in a "ready pool." 👉3. Running: A thread enters the Running state when the Thread Scheduler selects it from the Runnable pool. This is when the run() method code actually executes. Note: On a single-processor system, only one thread can be in the "Running" state at a time, though they switch so fast it looks like they are running simultaneously. 👉 4. Blocked / Waiting / Timed Waiting (Non-Runnable): A thread can temporarily enter a "Non-Runnable" state for several reasons: i) Blocked: Waiting to acquire a monitor lock (common in synchronized blocks). ii) Waiting: Waiting indefinitely for another thread to perform an action (using wait() or join()). iii) Timed Waiting: Waiting for a specific period (using sleep(milliseconds), wait(timeout), or join(timeout)). Once the condition is met (e.g., the sleep time ends or the lock is released), the thread moves back to the Runnable state. 👉5. Terminated (Dead): A thread enters the Terminated state when: i) It completes its execution (the run() method finishes). ii) It is stopped due to an unhandled exception or error. Once a thread is terminated, it cannot be restarted. ⚠️ Note (Error in the below image)📢: Once a thread reaches the TERMINATED state, it cannot transition to any other state. However, the image below incorrectly shows a transition from TERMINATED to TIMED WAITING, which is not possible.
To view or add a comment, sign in
-
-
Concurrency vs Parallelism in Java: Key Differences Explained Introduction to Java Java is now one of the most widely utilised and widespread languages of the world; it is high-level, object-oriented, and platform-independent. Java, which was developed by Sun Microsystems in 1995, was built with the principle of "write once, run anywhere", which implies that the code written in the Java language can run on any device that has the Java Virtual Machine (JVM) installed, regardless of any specific hardware or operating system. What Is Concurrency? Concurrency means managing several tasks at a given time; this does not mean running them at a single point in time, but managing them so they effectively make progress together. This is the way a program is made to respond while several things are done at once. "A woman standing behind the bar, serving multiple tables of a restaurant. She can't serve every customer at the same time, but she can try to switch between tables to satisfy the different customers. That's concurrency in action." In the case of Java, concurrency is most commonly implemented via threads that are lightweight units of execution, allowing different parts of the program to execute, but appear to execute simultaneously. What Is Parallelism? Parallelism is performing multiple things at the same time, using different processors or CPU cores for that purpose. The idea is not only to make the system responsive but also to speed it up–doing more in less time. For example, in an analogy of a restaurant, parallelism would be having different waiters serving different tables at the same time. In this way, each table would get simultaneous attention, making the service faster overall. It is in Java that you perform parallelism by executing tasks that can be split into independent units of work and can thus run across multiple CPU cores for true simultaneous execution. Can You Use Both Together? Indeed, many systems in the real world do have those properties. A Java application can be simultaneous and parallel at the same time; for example, a concurrent one would open multiple threads to handle various tasks: read, process, and save. Then, taking one of these threads, a parallel processing framework can be used to divide the workload among cores. When to Use Concurrency vs Parallelism Here's a simple rule: use concurrency when you want your application to handle many simultaneous tasks (especially I/O-bound tasks), even if they are not all active at any one point in time. Use parallelism when you want to speed up a single intensive task by dissecting it into parts that can be run across multiple cores. Some applications require one or the other. Most applications will need a combination of both. Knowing when and how to apply them is what separates a good Java developer from a great one.
To view or add a comment, sign in
-
-
⏳ Day 24 – 1 Minute Java Clarity – Exception Handling Basics** Try, Catch, Finally… Java's safety net! 🛡️ 📌 What is an Exception? An unexpected event that disrupts the normal flow of a program. 👉 Java handles this gracefully using try-catch blocks. 📌 Example: class ExceptionDemo { public static void main(String[] args) { try { int result = 10 / 0; // ArithmeticException } catch (ArithmeticException e) { System.out.println("Cannot divide by zero! " + e.getMessage()); } finally { System.out.println("This always runs ✅"); } } } 📌 Block Breakdown: ✔ try → Code that might throw an exception ✔ catch → Handles the exception ✔ finally → Always executes (clean up code) 💡 Real-time Example: Think of an ATM 🏧 try → You attempt a withdrawal catch → "Insufficient funds!" message shown finally → Card is always returned ✅ 📌 Exception Hierarchy: Throwable ├── Error (JVM level – don't handle) └── Exception ├── Checked (compile-time) └── Unchecked (runtime – RuntimeException) ⚠️ Interview Trap: Does finally always execute? 👉 YES — except when `System.exit()` is called or JVM crashes! 📌 Common Exceptions: ✔ ArithmeticException → divide by zero ✔ NullPointerException → null object access ✔ ArrayIndexOutOfBoundsException → invalid index ✔ ClassCastException → invalid type casting ✔ NumberFormatException → invalid string to number 💡 Quick Summary: ✔ try-catch prevents program crashes ✔ finally runs regardless of exception ✔ Checked exceptions must be handled at compile time ✔ Unchecked exceptions occur at runtime 🔹 Next Topic → throw vs throws in Java Did you know `finally` block even runs after a `return` statement? Drop 🔥 if this surprised you! #Java #ExceptionHandling #JavaProgramming #TryCatch #CoreJava #JavaDeveloper #BackendDeveloper #Coding #Programming #1MinuteJavaClarity #100DaysOfCode
To view or add a comment, sign in
-
-
Java-26 Problem with Old Java Concurrency (Executor + Future) In traditional Java: Future<Account> accountTask = executor.submit(...); Future<User> userTask = executor.submit(...); Future<Country> countryTask = executor.submit(...); Account acc = accountTask.get(); User user = userTask.get(); Country c = countryTask.get(); ❌ Issues:- If one task fails → others keep running (waste resources) You must manually cancel tasks No clear relationship between tasks Hard to debug 😵 Threads may leak 👉 Basically: Code does not reflect real workflow 🟢 What is Structured Concurrency? Structured concurrency treats concurrent tasks like a structured block (like a method call). 👉 All tasks: Start together End together Fail together ✅ New Way (Java 26 - Structured Concurrency) Example try (var scope = new StructuredTaskScope.ShutdownOnFailure()) { var accountTask = scope.fork(() -> fetchAccount()); var userTask = scope.fork(() -> fetchUser()); var countryTask = scope.fork(() -> fetchCountry()); scope.join(); // wait for all scope.throwIfFailed(); return new AccountSnapshot( accountTask.get(), userTask.get(), countryTask.get() ); } 🚀 Benefits 1. Automatic Failure Handling If one task fails → all others are cancelled automatically 2. No Thread Leaks Tasks are bound to scope → no orphan threads 3. Clean Lifecycle Start → Execute → Finish inside one block 4. Easier Debugging Clear parent-child relationship 5. Real-world Mapping Your code now looks like your logic: 👉 "Fetch all → combine → return" 🧠 Key Concept (Very Important) 👉 Old way = Unstructured (like goto statements) 👉 New way = Structured (like functions & loops) ⚡ Relation with Virtual Threads Virtual Threads (Java 21) → make concurrency cheap Structured Concurrency (Java 26) → make concurrency correct 👉 Together = 💥 Powerful backend systems 🔥 Real-Life Analogy Think of it like a team task: Old way: One member fails ❌ Others still working clueless 😐 Structured concurrency: One fails ❌ Whole team stops immediately ✔️ 🧾 Finally- Feature Old (Executor + Future) Structured Concurrency Error handling Manual Automatic Cancellation Manual Automatic Debugging Hard Easy Lifecycle Scattered Scoped Code clarity Low High
To view or add a comment, sign in
-
🚀🎊Day 78 of 90 – Java Backend Development ✨🎆 A Memory Leak in Java occurs when objects are no longer being used by the application, but the Garbage Collector (GC) is unable to remove them from the heap memory because they are still being unintentionally referenced. Even though Java has automatic memory management, a leak will slowly consume the available heap space until the JVM throws an OutOfMemoryError (OOM), causing the application to crash. 👉1. How it happens (The "GCRoot" Problem) In Java, an object is eligible for garbage collection if it is "unreachable." The GC starts from GC Roots (like local variables in the active thread or static variables) and traces all references. i) Normal Behaviour: When a method finishes, its local variables are cleared, the objects they pointed to become unreachable, and the GC reclaims the memory. ii) The Leak: An object is "logically" dead (your code doesn't need it anymore), but a "physically" live reference still exists (e.g., a forgotten item in a static List). Because a path still exists from a GC Root, the GC assumes the object is still important and leaves it alone. 👉 2. Common Causes in Java 👉Static Collections Static variables live for the entire lifetime of the JVM. If you add objects to a static List or Map and never remove them, they will stay in memory forever. public class Cache { private static Map<String, Object> map = new HashMap<>(); // If we never call map.remove(), this grows until OOM } 👉Unclosed resources: Connections to databases, files, or network sockets consume memory. If you don't call .close() (or use try-with-resources), the underlying native buffers may leak. 👉Inner classes: Non-static inner classes hold an implicit reference to their outer class. If the inner class object is passed around or stored, the outer class cannot be garbage collected, even if it's no longer used. 👉 Improper equals() and hashCode(): If you use custom objects as keys in a HashMap but don't implement equals() and hashCode() correctly, the Map won't find the duplicate keys. Instead, it will keep adding new entries every time you "update" a value, leading to a silent leak. 👉3. Symptoms of a memory Leak i) Performance Degradation: The GC runs more and more frequently (and takes longer) as it struggles to find free space, leading to "Stop-the-world" pauses. ii) Increased Memory Usage: The "Old Gen" (Tenured) space of the heap shows a steady upward trend in a sawtooth pattern that never returns to its baseline. iii) OutOfMemoryError: The application eventually crashes with java.lang.OutOfMemoryError: Java heap space. 👉 4. How to Detect and Fix To identify a leak, you need to look "inside" the JVM: i)Heap Dumps: Take a snapshot of the memory using jmap or via your IDE. ii)Analysis Tools: Use tools like Eclipse MAT (Memory Analyzer) or VisualVM. These tools show you which objects are taking up the most space and, more importantly, the "Path to GC Root" that is keeping them alive.
To view or add a comment, sign in
-
-
🚀 Java Practice: Custom Exception Handling I’ve been strengthening my core Java skills by practicing Custom Exception Handling, and here’s a small program I built based on a real-world scenario — Driving License Eligibility System. 💡 Concept Used: User-defined (custom) exceptions Exception propagation using throws Handling exceptions with try-catch Real-time validation logic 📌 Scenario: The program checks whether a person is eligible for a driving license based on age: ✅ 18–65 → Eligible ❌ Below 18 → UnderAgeException ❌ Above 65 → OverAgeException ⚙️ What I implemented: Created two custom exception classes: UnderAgeException OverAgeException Used meaningful error messages by overriding getMessage() Structured the program using multiple classes for clarity and modularity 🎯 Key Learning: Custom exceptions help in creating more readable, maintainable, and real-world oriented programs instead of relying only on predefined exceptions. 📚 This practice improved my understanding of how Java handles errors and how we can control program flow effectively. package exception; import java.util.Scanner; public class Goi_Custum_Exception { public static void main(String[] args) { GovOFKarnataka gk = new GovOFKarnataka(); gk.init(); } } class GovOFKarnataka{ void init() { Rto r = new Rto(); r.acceptInput(); try { r.verify(); } catch (Exception e) { // TODO Auto-generated catch block System.out.println(e.getMessage()); } } } class Rto{ int age; void acceptInput() { Scanner scan=new Scanner(System.in); System.out.println("Enter age: "); age=scan.nextInt(); } void verify() throws Exception{ if(age>=18&&age<=65) { System.out.println("Eligible for Driving Licence 🤩"); } else if(age>=65){ Exception oa = new OverAgeException(); throw oa; } else { Exception ua = new UnderAgeException(); throw ua; } } } class UnderAgeException extends Exception{ @Override public String getMessage() { return "Under Age Come After 18"; } } class OverAgeException extends Exception{ @Override public String getMessage() { // TODO Auto-generated method stub return "Over age you can go on others vehicle 😊"; } }
To view or add a comment, sign in
-
🔥 Day 22: Deadlock in Java (with Example 🔥) One of the most dangerous problems in multithreading — and a favorite interview topic 👇 🔹 What is Deadlock? 👉 Definition: A situation where two or more threads are stuck forever, waiting for each other’s resources. 🔹 Simple Real-Life Example 👤 Person A holds Resource 1 and waits for Resource 2 👤 Person B holds Resource 2 and waits for Resource 1 👉 Result = ❌ No one proceeds (Deadlock) 🔹 Java Example class DeadlockExample { public static void main(String[] args) { final Object lock1 = new Object(); final Object lock2 = new Object(); Thread t1 = new Thread(() -> { synchronized(lock1) { System.out.println("Thread 1: Holding lock1"); try { Thread.sleep(100); } catch (Exception e) {} synchronized(lock2) { System.out.println("Thread 1: Holding lock2"); } } }); Thread t2 = new Thread(() -> { synchronized(lock2) { System.out.println("Thread 2: Holding lock2"); try { Thread.sleep(100); } catch (Exception e) {} synchronized(lock1) { System.out.println("Thread 2: Holding lock1"); } } }); t1.start(); t2.start(); } } 🔹 What Happens? ✔ Thread 1 locks lock1 ✔ Thread 2 locks lock2 ❌ Both wait for each other → Deadlock 🔹 Deadlock Visualization Thread 1 → lock1 → waiting for lock2 Thread 2 → lock2 → waiting for lock1 🔹 Conditions for Deadlock (Must Have All 4) 1️⃣ Mutual Exclusion 2️⃣ Hold and Wait 3️⃣ No Preemption 4️⃣ Circular Wait 🔹 How to Prevent Deadlock? ✔ Always lock resources in same order ✔ Use tryLock() (from ReentrantLock) ✔ Avoid nested locks when possible ✔ Use timeouts 🔹 Key Takeaway 👉 Deadlock doesn’t crash your program — it freezes it forever ❗ 💡 Pro Tip: Consistent lock ordering is the easiest and most effective solution 🚀 📌 Final Thought: "Deadlock is not an error — it’s a silent killer in multithreading." #Java #Multithreading #Deadlock #Concurrency #Programming #JavaDeveloper #Coding #InterviewPrep #Day22
To view or add a comment, sign in
-
-
Day 15 Java Practice: Find Product with Maximum Total Quantity While practicing Java, I worked on a small problem involving strings, arrays, and maps. 👉 Given product data with quantity like: {"xyz 19","abc 30","xyz 21","abc 23"} The goal was to: Sum the quantities for the same product Find which product has the maximum total quantity 🧠 Approach: Split each string to get product name and quantity Store and add quantities using a HashMap Traverse the map to find the maximum value ============================================= // Online Java Compiler // Use this editor to write, compile and run your Java code online import java.util.*; class Main { public static void main(String[] args) { String a []={"xyz 19","abc 30","xyz 21","abc 23"}; Map<String,Integer>hmap=new HashMap<String,Integer>(); for(String s : a) { String data []=s.split(" "); String name=data[0]; int value=Integer.parseInt(data[1]); hmap.put(name,hmap.getOrDefault(name,0)+value); } int max=0; String result=""; for(Map.Entry<String,Integer>entry:hmap.entrySet()) { if(entry.getValue()>max) { max=entry.getValue(); result=entry.getKey(); } } System.out.println(result+" "+max); } } Output: abc 53 #JavaDeveloper #AutomationEngineer #CodingPractice #Collections #ProblemSolving #Learning
To view or add a comment, sign in
-
More from this author
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development