So far, everything we wrote ran in a single thread. One path. One execution flow. But modern applications don’t work that way. They: • Handle multiple users • Perform background tasks • Process data in parallel That’s where 𝗠𝘂𝗹𝘁𝗶𝘁𝗵𝗿𝗲𝗮𝗱𝗶𝗻𝗴 comes in. A thread is a lightweight unit of execution. In Java, you can create one by: class MyTask extends Thread { public void run() { System.out.println("Task running"); } } Or using Runnable: Runnable task = () -> System.out.println("Running"); new Thread(task).start(); But threads introduce new challenges: • Race conditions • Shared memory conflicts • Synchronization issues Concurrency is not just about speed. It’s about coordination. Today was about: • Understanding what a thread is • Creating threads in Java • Realizing why concurrency is difficult Parallel execution increases performance. But it also increases responsibility. #Java #Multithreading #Concurrency #SoftwareEngineering #Programming #LearningInPublic
Java Multithreading: Understanding Threads and Concurrency
More Relevant Posts
-
GC does not remove objects because they are “old.” It removes objects that are unreachable. That is the key idea. The JVM starts from GC Roots like: stack variables, static fields, active threads, and JNI references. Then it checks: Is there still a live path from any GC Root to this object? If yes, the object stays alive. If no, it becomes eligible for garbage collection. So “orphan” or “zombie” object usually means: an object that no longer has any reachable reference path from GC Roots. How do you keep an object from removal? Keep it reachable. Common ways: hold a live reference store it in a collection keep it in a static field reference it from another live object But there is an important warning: If you keep references longer than needed, GC cannot free them. That is how memory leaks often happen in Java too. The easiest rule to remember is: Reachable = alive Unreachable = collectible Which JVM topic should I explain next: GC roots, heap vs stack, or weak references? #Java #JVM #GarbageCollection #JavaDeveloper #BackendDevelopment #SoftwareEngineering #MemoryManagement #Programming #TechLearning #ComputerScience
To view or add a comment, sign in
-
-
🚀 Day 540 of #750DaysOfCode 🚀 Today I solved Maximum Non Negative Product in a Matrix (LeetCode 1594) using Java. 🔹 Problem Summary: We are given a matrix where we start from the top-left corner and can move only right or down. Among all possible paths to the bottom-right corner, we must find the maximum non-negative product of the values along the path. If every possible path gives a negative product, we return -1. 🔹 Approach Used (Dynamic Programming): This problem is tricky because the grid contains negative numbers. A negative value can turn the smallest product into the largest, so we must keep track of both: • Maximum product till each cell • Minimum product till each cell For every position, I calculated possible values from the top and left cells and updated both max and min DP tables. At the end, the bottom-right cell gives the answer. 🔹 Key Concepts Learned: ✅ Dynamic Programming on Grid ✅ Handling negative numbers in DP ✅ Keeping both max and min states ✅ Matrix path problems ✅ Modulo handling for large numbers Problems like this show why DP is powerful when greedy approaches fail. #750DaysOfCode #Day540 #LeetCode #Java #DSA #Algorithms #DynamicProgramming #ProblemSolving #CodingChallenge #Consistency
To view or add a comment, sign in
-
-
Day 14/30 – LeetCode streak Today’s problem: Concatenation of Consecutive Binary Numbers You need the decimal value of '1' + '2' + '3' + ... + 'n' written in binary back-to-back, all under mod (10^9 + 7). Core trick: treat “concatenate in binary” as shift + OR: * When you append 'i' to the right, you’re really shifting the current result left by 'bits(i)' and OR-ing 'i' into the free space. * The number of bits only increases when 'i' hits a power of two (1, 2, 4, 8, …), so you just track 'bits' and bump it whenever '(i & (i - 1)) == 0'. Day 14 takeaway: Once you see that “stick this binary to the right” is the same as “shift by its bit length and OR”, the whole problem becomes a clean for-loop plus the power-of-two trick—no string building or big integer juggling needed. #leetcode #dsa #java #bitmanipulation #consistency
To view or add a comment, sign in
-
-
Day 6/100 – LeetCode Challenge 🚀 Problem: #189 Rotate Array Difficulty: Medium Language: Java Approach: Array Reversal Technique Time Complexity: O(n) Space Complexity: O(1) 🔍 Key Insight: Instead of shifting elements one by one, the array can be rotated efficiently using a three-step reversal strategy. Steps: 1️⃣ Reverse the entire array 2️⃣ Reverse the first k elements 3️⃣ Reverse the remaining elements This achieves the required rotation in-place with constant extra space. 🧠 Solution Brief: Calculated k % n to handle cases where k is greater than array length. Reversed the entire array first. Then reversed the first k elements and the remaining n-k elements. This sequence correctly rotates the array to the right. 📌 What I Learned: Understanding patterns like array reversal can simplify problems that initially seem complex. Optimizing from brute force shifting to an in-place O(n) solution improves efficiency. #LeetCode #Day6 #100DaysOfCode #Java #DSA #Arrays #RotateArray #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
I almost overcomplicated this problem… until I realized it was just prefix sum. 👉 Equal Sum Grid Partition (LeetCode 3546) At first, I thought: “Try all possible cuts.” But that quickly gets messy. Then the real insight hit: If the total sum is even, we just need to find a prefix (row-wise or column-wise) equal to total / 2. That’s it. No brute force. Just clarity. 💡 Lesson: The problem is not about splitting the grid — it’s about finding the right prefix. Time: O(m × n) Space: O(n) Sharing the clean Java solution on GitHub 👇 #leetcode #dsa #java #coding #developers #softwareengineer #codewithishwar
To view or add a comment, sign in
-
Imagine this situation. You and your friend are working on a 𝐬𝐡𝐚𝐫𝐞𝐝 𝐰𝐡𝐢𝐭𝐞𝐛𝐨𝐚𝐫𝐝. You write a number on the board. Your friend reads it. Simple, right? But now imagine both of you are working 𝐢𝐧 𝐝𝐢𝐟𝐟𝐞𝐫𝐞𝐧𝐭 𝐫𝐨𝐨𝐦𝐬 with 𝐲𝐨𝐮𝐫 𝐨𝐰𝐧 𝐜𝐨𝐩𝐢𝐞𝐬 𝐨𝐟 𝐭𝐡𝐞 𝐛𝐨𝐚𝐫𝐝. You update your board. But your friend still sees the 𝐨𝐥𝐝 𝐯𝐚𝐥𝐮𝐞. This is exactly what can happen in 𝗺𝘂𝗹𝘁𝗶𝘁𝗵𝗿𝗲𝗮𝗱𝗲𝗱 𝗝𝗮𝘃𝗮 𝗽𝗿𝗼𝗴𝗿𝗮𝗺𝘀. Each thread may work with its 𝐨𝐰𝐧 𝐜𝐚𝐜𝐡𝐞𝐝 𝐜𝐨𝐩𝐲 𝐨𝐟 𝐯𝐚𝐫𝐢𝐚𝐛𝐥𝐞𝐬 instead of the shared memory. And that’s where the 𝗝𝗮𝘃𝗮 𝗠𝗲𝗺𝗼𝗿𝘆 𝗠𝗼𝗱𝗲𝗹 (𝗝𝗠𝗠) comes in. What is the Java Memory Model? The 𝗝𝗮𝘃𝗮 𝗠𝗲𝗺𝗼𝗿𝘆 𝗠𝗼𝗱𝗲𝗹 defines how: • Threads interact with memory • Changes made by one thread become visible to others • The JVM handles caching and reordering Without rules like this, concurrent programs would behave unpredictably. 𝐓𝐡𝐞 𝐕𝐢𝐬𝐢𝐛𝐢𝐥𝐢𝐭𝐲 𝐏𝐫𝐨𝐛𝐥𝐞𝐦 Example: class FlagExample { static boolean running = true; public static void main(String[] args) { new Thread(() -> { while (running) { // waiting } System.out.println("Thread stopped"); }).start(); running = false; } } You might expect the thread to stop immediately. But sometimes 𝗶𝘁 𝗸𝗲𝗲𝗽𝘀 𝗿𝘂𝗻𝗻𝗶𝗻𝗴 𝗳𝗼𝗿𝗲𝘃𝗲𝗿. Why? Because the thread may keep reading a 𝗰𝗮𝗰𝗵𝗲𝗱 𝘃𝗮𝗹𝘂𝗲 𝗼𝗳 𝗿𝘂𝗻𝗻𝗶𝗻𝗴. It never sees the update. The Fix → 𝐯𝐨𝐥𝐚𝐭𝐢𝐥𝐞 volatile static boolean running = true; Now Java guarantees: • Updates are visible to all threads • Threads read the latest value from memory 𝐯𝐨𝐥𝐚𝐭𝐢𝐥𝐞 ensures 𝘃𝗶𝘀𝗶𝗯𝗶𝗹𝗶𝘁𝘆, not locking. 𝐊𝐞𝐲 𝐈𝐝𝐞𝐚 Multithreading problems are not always about race conditions. Sometimes the issue is simply: • Threads not seeing the same data. Today was about understanding: • Why threads may see 𝗱𝗶𝗳𝗳𝗲𝗿𝗲𝗻𝘁 𝘃𝗮𝗹𝘂𝗲𝘀 • What the 𝗝𝗮𝘃𝗮 𝗠𝗲𝗺𝗼𝗿𝘆 𝗠𝗼𝗱𝗲𝗹 controls • How 𝐯𝐨𝐥𝐚𝐭𝐢𝐥𝐞 ensures 𝘃𝗶𝘀𝗶𝗯𝗶𝗹𝗶𝘁𝘆 Concurrency is not only about 𝗿𝘂𝗻𝗻𝗶𝗻𝗴 𝘁𝗵𝗶𝗻𝗴𝘀 𝗳𝗮𝘀𝘁𝗲𝗿. It’s also about 𝗺𝗮𝗸𝗶𝗻𝗴 𝘀𝘂𝗿𝗲 𝗲𝘃𝗲𝗿𝘆 𝘁𝗵𝗿𝗲𝗮𝗱 𝘀𝗲𝗲𝘀 𝘁𝗵𝗲 𝘀𝗮𝗺𝗲 𝗿𝗲𝗮𝗹𝗶𝘁𝘆. #Java #JavaConcurrency #JavaMemoryModel #Multithreading #SoftwareEngineering #LearningInPublic #Programming
To view or add a comment, sign in
-
-
"Architecting Knowledge" - Java Wisdom Series Post #13: Sealed Classes - Controlling Your Hierarchy 👇 Open inheritance is technical debt. Here's how to control it. Why This Matters: Sealed classes restrict who can extend your hierarchy, making the set of subtypes finite and known. The compiler can verify exhaustiveness in pattern matching - no default clause needed. When you add a new permitted subclass, every switch breaks until you handle the new case. This turns runtime errors into compile-time errors. Key Takeaway: Use sealed classes when you own the complete set of implementations and want compiler-verified exhaustiveness. Perfect for state machines, result types, and domain models with finite variants. #Java #JavaWisdom #SealedClasses #PatternMatching #ModernJava #TypeSafety Are you still using open inheritance for finite type sets? Time to seal them! All code examples on GitHub - bookmark for quick reference: https://lnkd.in/dJUx3Rd3
To view or add a comment, sign in
-
-
Day 8 Today I solved “Check if Binary String Has at Most One Segment of Ones” on LeetCode. At first glance, the problem looks simple: Given a binary string, check whether it contains only one continuous segment of '1's. Example: "110" → Valid (one segment of 1s) "1001" → Invalid (two separate segments of 1s) While solving it, I realized an interesting observation: 👉 If a binary string has more than one segment of 1s, the pattern "01" must appear before another "1". So instead of counting segments explicitly, we can simply check whether '01' appears and is followed by another '1'. This turns the problem into a very efficient linear scan with O(n) time complexity. 💡 Key takeaway: Sometimes the simplest solution comes from recognizing patterns in the string rather than simulating the whole process. Also happy to see my solution running at 0 ms runtime (100% faster) 🚀 #LeetCode #DSA #ProblemSolving #Java #CodingJourney #BinaryStrings
To view or add a comment, sign in
-
-
🚀 Day 20/180 | #180DaysOfCode 📍 LeetCode | 💻 Java Solved: 350. Intersection of Two Arrays II Used sorting + two-pointer technique to efficiently find common elements appearing in both arrays while maintaining correct frequency. ⏱️ Time Complexity: O(n log n + m log m) 📦 Space Complexity: O(min(n, m)) (for storing the intersection) Strengthening understanding of array traversal and two-pointer pattern through consistent practice. 💪 Consistency keeps the progress moving 🚀 #DSA #LeetCode #Java #CodingJourney #Consistency
To view or add a comment, sign in
-
-
Day 9/100 – LeetCode Challenge 🚀 Problem: #2022 Convert 1D Array Into 2D Array Difficulty: Easy Language: Java Approach: Sequential Mapping with Nested Loop Time Complexity: O(m × n) Space Complexity: O(m × n) 🔍 Key Insight: A 2D array of size m × n can only be formed if the total number of elements equals m × n. If original.length ≠ m × n, it is impossible to construct the matrix. 🧠 Solution Brief: First checked whether m × n equals the length of the original array. If not equal, returned an empty 2D array. Otherwise created a new 2D matrix and filled it sequentially using nested loops while tracking the current index in the 1D array. 📌 What I Learned: Validating constraints early simplifies the solution. Mapping between 1D and 2D arrays becomes straightforward when using a simple index pointer. #LeetCode #Day9 #100DaysOfCode #Java #DSA #Arrays #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
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