Java Multithreading Mystery: Shared Object Confusion

🚀 Day 7 — The Multithreading Mystery That Breaks Developer Logic 🧩 Every Java developer says: “I know how threads work.” But when two threads share the same object… even pros get confused about what actually happens 👇 class Printer implements Runnable {   int count = 0;   @Override   public void run() {     for (int i = 0; i < 3; i++) {       System.out.println(Thread.currentThread().getName() + " → " + count++);     }   }   public static void main(String[] args) {     Printer printer = new Printer();     Thread t1 = new Thread(printer, "Thread-A");     Thread t2 = new Thread(printer, "Thread-B");     t1.start();     t2.start();   } } 💭 Question: What could be the possible output? 1️⃣ Each thread prints 0 1 2 independently 2️⃣ The count value increases continuously (shared between threads) 3️⃣ Compile-time error 4️⃣ Unpredictable output 💬 Drop your guess in the comments 👇 Most devs think they know the answer — until they realize what “shared object” actually means in Java threading 😵💫 Can you explain why it happens? 🧠 #Java #Multithreading #Concurrency #CodingChallenge #JavaDeveloper #InterviewQuestion #Day7Challenges #SpringBoot

Both the thread use same object That’s why the possible output will be 0 1 2 3 4 5 4)Option is correct

To view or add a comment, sign in

Explore content categories