Vinay Yadav’s Post

🚀 Day 2 – Creating Threads & Understanding Execution Control in Java Today I focused on one of the most misunderstood yet critical concepts in Java Multithreading — how threads are actually created and how start() works internally. Understanding this difference is essential for writing scalable backend systems. 🔄 Two Ways to Create a Thread 1️⃣ Extending Thread Class • Override run() • Create object of class • Call start() 2️⃣ Implementing Runnable (Recommended Approach) • Implement Runnable interface • Override run() • Pass Runnable object to Thread • Call start() 💡 Why Runnable is Recommended? ✔ Supports multiple inheritance (since Java doesn’t allow extending multiple classes) ✔ Clear separation of task and execution mechanism ✔ Foundation of Executor Framework ✔ Cleaner architecture for scalable systems In real-world backend development, we rarely create raw threads manually. We use: • ThreadPool • ExecutorService • CompletableFuture / Async APIs Because uncontrolled thread creation can degrade performance or even crash production systems. 🚦 start() vs run() – A Critical Interview Concept This is one of the most common interview traps. If run() is called directly: • Executes inside current thread • No new thread is created • No parallel execution If start() is called: • Thread is registered with JVM • Required resources are allocated • New call stack is created • Internally invokes run() 👉 A new thread is created only when start() is called. Using run() instead of start() can silently break scalability. 🔎 Thread.currentThread() Thread.currentThread() returns the currently executing thread object. Used for: • Logging thread names in production • Debugging concurrency issues • Monitoring execution flow This becomes extremely useful in backend systems handling multiple concurrent requests. ⚙ Thread Properties We can modify: • setName(String) • setPriority(int) Important: Thread ID is assigned by JVM and is read-only. Meaningful thread naming in production helps trace issues faster. 🏗 Backend Perspective Imagine 1000 users hitting a login API simultaneously. If developer mistakenly uses run() instead of start(): • Application behaves like single-threaded • Throughput drops • Server load increases • Performance degrades Correct thread handling directly impacts backend scalability. 🎯 Key Interview Topics Covered • Runnable vs Thread – which is better and why? • Difference between start() and run() • What happens internally when start() is called? • Can a thread be started twice? • Why use thread pools in backend systems? 📌 Today’s Learning Concurrency design matters more than syntax. Backend performance depends on: • Correct thread creation • Proper execution control • Scalable concurrency management Still strengthening fundamentals to build better backend systems. 🚀 #Java #Multithreading #BackendDevelopment #Concurrency #Threading #InterviewPreparation #SoftwareEngineering #LearningJourney

  • graphical user interface, text, application, chat or text message

To view or add a comment, sign in

Explore content categories