PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम Multithreading: Thread Class vs Runnable Interface (मल्टी-थ्रेडिंग: Thread क्लास एक्सटेंड करना बनाम Runnable इम्प्लीमेंट) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।
Overview: Java enables concurrent execution through multithreading either by subclassing java.lang.Thread or by implementing the java.lang.Runnable functional interface. Implementing Runnable is the preferred design because it decouples the unit of work from thread management, supports extending another class, and integrates cleanly with lambdas and Executors.
अध्याय सार: जावा में मल्टी-थ्रेडिंग द्वारा समानांतर कार्य करने के दो मुख्य तरीके हैं: java.lang.Thread क्लास को एक्सटेंड करना या java.lang.Runnable इंटरफेस को इम्प्लीमेंट करना। Runnable को बेहतर माना जाता है क्योंकि यह कार्य को थ्रेड प्रबंधन से अलग रखता है, दूसरी क्लास को एक्सटेंड करने की स्वतंत्रता देता है और लैम्ब्डा व ExecutorService के साथ आसानी से काम करता है।
Multithreading: Thread Class vs Runnable Interface: Detailed Notes & Architecture (मल्टी-थ्रेडिंग: Thread क्लास एक्सटेंड करना बनाम Runnable इम्प्लीमेंट)
1. Multithreading Architecture and Thread Memory Model (मल्टी-थ्रेडिंग आर्किटेक्चर और थ्रेड मेमोरी मॉडल)
[EN] A thread is the smallest lightweight unit of execution inside a process. All threads within one JVM process share the common Heap (objects) and Method Area (class metadata), enabling fast inter-thread data sharing without costly IPC overhead. However, every thread receives its own private JVM Call Stack (local variables, method frames) and Program Counter (PC) register to track independent execution flow. Context switching between threads of the same process is far lighter than switching between separate OS processes.
[HI] थ्रेड किसी प्रोसेस के भीतर निष्पादन की सबसे छोटी हल्की इकाई है। एक JVM प्रोसेस के सभी थ्रेड कॉमन हीप और मेथड एरिया को साझा करते हैं, जिससे डेटा साझा करना तेज होता है। हालांकि प्रत्येक थ्रेड का अपना अलग प्राइवेट JVM कॉल स्टैक और प्रोग्राम काउंटर (PC) रजिस्टर होता है। थ्रेड्स के बीच कॉन्टेक्स्ट स्विचिंग अलग-अलग प्रोसेस की तुलना में काफी हल्की होती है।
2. Approach 1: Extending the java.lang.Thread Class (तरीका 1: java.lang.Thread क्लास को एक्सटेंड करना)
[EN] In the inheritance approach, a custom class extends Thread and overrides public void run() to define the task. Creating an instance creates a new Thread object in NEW state, and calling t.start() triggers thread allocation. The critical drawback is Java's single-inheritance restriction: once a class extends Thread, it cannot extend any other application base class (like Employee or JFrame). Additionally, each object becomes a separate thread instance, tightly coupling business logic with thread lifecycle mechanics.
[HI] इनहेरिटेंस दृष्टिकोण में एक कस्टम क्लास Thread को एक्सटेंड करती है और कार्य परिभाषित करने के लिए public void run() को ओवरराइड करती है। इसका बड़ा नुकसान जावा की सिंगल क्लास इनहेरिटेंस सीमा है: Thread को एक्सटेंड करने के बाद वह क्लास किसी अन्य बेस क्लास को एक्सटेंड नहीं कर सकती। साथ ही यह बिजनेस लॉजिक को थ्रेड लाइफसाइकिल से जकड़ देता है।
3. Approach 2: Implementing Runnable Interface and Lambda Support (तरीका 2: Runnable इंटरफेस इम्प्लीमेंटेशन और लैम्ब्डा सपोर्ट)
[EN] java.lang.Runnable is a @FunctionalInterface (marked in Java 8) containing a single abstract method: void run(). A class implementing Runnable can still extend another parent class and pass the same Runnable instance into multiple Thread constructors (new Thread(task, "Worker-1")) so threads share one task object naturally. Because Runnable is a functional interface, tasks can be passed concisely via lambdas like new Thread(() -> doWork()).start(), and Runnable tasks submit directly to ExecutorService thread pools.
[HI] java.lang.Runnable एक @FunctionalInterface है जिसमें केवल एक एब्स्ट्रैक्ट मेथड void run() होता है। Runnable इम्प्लीमेंट करने वाली क्लास दूसरी पैरेंट क्लास को एक्सटेंड कर सकती है और एक ही Runnable ऑब्जेक्ट को कई Thread कंस्ट्रक्टर्स में साझा किया जा सकता है। फंक्शनल इंटरफेस होने के कारण इसे लैम्ब्डा एक्सप्रेशन और ExecutorService थ्रेड पूल के साथ आसानी से उपयोग किया जा सकता है।
4. start() vs run() Trap and Runnable vs Callable (start() बनाम run() की गलती और Runnable बनाम Callable)
[EN] Calling t.start() registers the thread with the Thread Scheduler, allocates a new call stack, and invokes run() asynchronously on the new thread. Calling t.run() directly behaves as an ordinary synchronous method call on the caller thread (e.g., main thread) without spawning any concurrency! Invoking start() a second time on the same Thread object throws IllegalThreadStateException. For tasks that must return a computed result or throw checked exceptions, Java 5 introduced Callable<V> (call() throws Exception), executed via ExecutorService to return a Future<V>.
[HI] t.start() कॉल करने पर थ्रेड शेड्यूलर में रजिस्टर होता है, नया कॉल स्टैक बनता है और run() नए थ्रेड पर एसिंक्रोनस तरीके से चलता है। सीधे t.run() कॉल करने पर कोई नया थ्रेड नहीं बनता, बल्कि वह सामान्य मेथड की तरह वर्तमान कॉलर थ्रेड पर ही चल जाता है! एक ही थ्रेड पर दूसरी बार start() कॉल करने पर IllegalThreadStateException आता है। परिणाम लौटाने या चेक्ड एक्सेप्शन फेंकने के लिए जावा 5 में Callable<V> (Future<V> के साथ) दिया गया है।
Syntax, Formula & Technical Example (Multithreading: Thread Class vs Runnable Interface)
// 1. Extending Thread vs 2. Implementing Runnable (Lambda)
class MyThread extends Thread {
public void run() { System.out.println("Via Thread: " + getName()); }
}
Runnable task = () -> System.out.println("Via Runnable: " + Thread.currentThread().getName());
new MyThread().start(); // Spawns new stack & thread
new Thread(task, "T2").start(); // Preferred: decoupled & reusable
Multithreading: Thread Class vs Runnable Interface Quick Reference & Comparison Table
| Feature / Aspect | Extending Thread Class | Implementing Runnable Interface |
|---|---|---|
| Inheritance Flexibility | Cannot extend any other class (single inheritance) | Can extend another class and implement interfaces |
| Design Coupling | Couples task logic with Thread object lifecycle | Separates task from thread execution & Executor pools |
| Resource Sharing | Each thread creates its own separate object | Single Runnable instance can be shared by many threads |
| Java 8 Lambda & Result | Not a functional interface; void run() | @FunctionalInterface lambda-ready; void run() |
Multithreading: Thread Class vs Runnable Interface Top 20 Bilingual MCQs with Answers
Q1. What happens if you call t.run() directly instead of t.start() on a Thread object t?
(यदि आप Thread ऑब्जेक्ट t पर t.start() के बजाय सीधे t.run() कॉल करते हैं तो क्या होता है?)
(A) JVM skips executing run() completely
(B) A new thread is started with lowest priority
(C) Throws IllegalThreadStateException
(D) run() executes synchronously on the current calling thread without starting a new thread
Correct Answer: (D)
[EN] Calling run() directly is just a normal Java method invocation on the caller's stack; only start() spawns a new thread.
[HI] सीधे run() कॉल करना एक सामान्य मेथड कॉल की तरह वर्तमान थ्रेड पर ही चलता है; नया थ्रेड केवल start() से बनता है।
Q2. Which advantage makes implementing Runnable generally preferred over extending Thread?
(किस लाभ के कारण Thread एक्सटेंड करने के बजाय Runnable इम्प्लीमेंट करना बेहतर माना जाता है?)
(A) The class remains free to extend another base class and works with Executor thread pools
(B) Runnable allows returning a value from run()
(C) Runnable automatically synchronizes all variables
(D) Runnable runs twice as fast in the JIT compiler
Correct Answer: (A)
[EN] Java allows extending only one class, so implementing Runnable avoids wasting the sole inheritance slot and separates task from runner.
[HI] जावा में एक ही क्लास एक्सटेंड की जा सकती है, इसलिए Runnable चुनने से दूसरी क्लास एक्सटेंड करने की स्वतंत्रता बची रहती है।
Q3. What exception is thrown if t.start() is invoked twice on the same Thread instance?
(एक ही Thread इंस्टेंस पर दो बार t.start() कॉल करने पर कौन सा एक्सेप्शन उत्पन्न होता है?)
(A) ConcurrentModificationException
(B) InterruptedException
(C) ThreadDeathError
(D) IllegalThreadStateException
Correct Answer: (D)
[EN] Once a Thread leaves the NEW state via start(), calling start() again throws IllegalThreadStateException.
[HI] एक बार NEW अवस्था से बाहर निकलने के बाद दोबारा start() कॉल करने पर IllegalThreadStateException आता है।
Q4. Which memory areas inside a JVM are private per thread rather than shared across all threads?
(JVM के भीतर कौन से मेमोरी क्षेत्र सभी थ्रेड्स में साझा होने के बजाय प्रत्येक थ्रेड के लिए निजी (Private) होते हैं?)
(A) Heap and Method Area
(B) String Constant Pool and Heap
(C) Static variables and Heap
(D) JVM Stack and Program Counter (PC) Register
Correct Answer: (D)
[EN] Heap and Method Area (Metaspace) are shared across threads, while each thread maintains its own Stack and PC register.
[HI] हीप और मेथड एरिया साझा होते हैं, जबकि प्रत्येक थ्रेड का अपना अलग JVM स्टैक और प्रोग्राम काउंटर रजिस्टर होता है।
Q5. Which interface must a background task implement if it needs to return a result and throw checked exceptions?
(यदि किसी बैकग्राउंड टास्क को एक रिजल्ट लौटाना हो और चेक्ड एक्सेप्शन फेंकना हो, तो उसे कौन सा इंटरफेस इम्प्लीमेंट करना चाहिए?)
(A) java.util.Observer
(B) java.lang.Runnable
(C) java.lang.ThreadGroup
(D) java.util.concurrent.Callable<V>
Correct Answer: (D)
[EN] Runnable.run() has void return and cannot throw checked exceptions, whereas Callable<V>.call() returns V and throws Exception.
[HI] Callable<V> का call() मेथड V टाइप का मान लौटाता है और Exception फेंक सकता है, जबकि Runnable.run() void होता है।
Q6. In which package are the Thread class and Runnable interface located?
(Thread क्लास और Runnable इंटरफेस किस पैकेज में स्थित हैं?)
(A) java.sys
(B) java.lang
(C) java.util.concurrent
(D) java.io
Correct Answer: (B)
[EN] Both Thread and Runnable reside in java.lang and require no explicit import statement.
[HI] Thread और Runnable दोनों java.lang पैकेज में हैं और इनके लिए किसी import की आवश्यकता नहीं होती।
Q7. What does Thread.currentThread().getName() print when a task is executed via t.run() from main()?
(यदि main() के अंदर से t.run() कॉल किया जाए तो Thread.currentThread().getName() क्या प्रिंट करेगा?)
(A) main
(B) Runnable-0
(C) Thread-0
(D) null
Correct Answer: (A)
[EN] Because t.run() did not spawn a new thread, the executing thread is still the main thread.
[HI] चूंकि t.run() नया थ्रेड नहीं बनाता, इसलिए निष्पादन करने वाला वर्तमान थ्रेड main ही रहता है।
Q8. What is the default naming pattern assigned by JVM to user threads created via new Thread() without an explicit name?
(बिना नाम दिए new Thread() से बनाए गए थ्रेड्स को JVM डिफ़ॉल्ट रूप से किस पैटर्न का नाम देता है?)
(A) Worker-1, Worker-2, ...
(B) Task-0, Task-1, ...
(C) Thread-0, Thread-1, ...
(D) JVM-Thread-1, JVM-Thread-2, ...
Correct Answer: (C)
[EN] The Thread constructor maintains an atomic counter assigning default names starting at Thread-0.
[HI] Thread कंस्ट्रक्टर डिफ़ॉल्ट रूप से Thread-0, Thread-1 जैसे नाम आवंटित करता है।
Q9. Why is java.lang.Runnable eligible to be written as a Lambda expression in Java 8+?
(जावा 8+ में java.lang.Runnable को लैम्ब्डा एक्सप्रेशन के रूप में क्यों लिखा जा सकता है?)
(A) Thread overrides its lambda method internally
(B) It is annotated with @FunctionalInterface and has exactly one abstract method run()
(C) It contains only static methods
(D) It extends Function<Void, Void>
Correct Answer: (B)
[EN] Any interface with a single abstract method (SAM) is a functional interface usable with lambda expressions.
[HI] Runnable में केवल एक एब्स्ट्रैक्ट मेथड run() है और यह @FunctionalInterface है, इसलिए यह लैम्ब्डा के योग्य है।
Q10. What happens when you pass null as the Runnable target to new Thread((Runnable) null).start()?
(new Thread((Runnable) null).start() चलाने पर क्या होता है?)
(A) Runs forever in an infinite loop
(B) The Thread starts normally and its default run() checks if target != null, doing nothing and terminating
(C) Throws NullPointerException in constructor
(D) Throws IllegalArgumentException on start()
Correct Answer: (B)
[EN] Thread's default run() safely checks `if (target != null) target.run();`, so a null Runnable simply finishes immediately.
[HI] Thread का डिफ़ॉल्ट run() target != null जांचता है; null होने पर यह बिना एरर के तुरंत समाप्त हो जाता है।
Q11. Which method marks a thread as a background service thread that dies automatically once all user threads finish?
(कौन सा मेथड किसी थ्रेड को बैकग्राउंड डेमन (Daemon) थ्रेड बनाता है जो सभी यूजर थ्रेड समाप्त होते ही बंद हो जाता है?)
(A) t.setBackground(true)
(B) t.setDaemon(true) before calling t.start()
(C) t.setPriority(0)
(D) t.setDaemon(true) after calling t.start()
Correct Answer: (B)
[EN] setDaemon(true) must be called before start(); calling it on an already started thread throws IllegalThreadStateException.
[HI] start() कॉल करने से पहले t.setDaemon(true) सेट करना होता है; सभी यूजर थ्रेड बंद होने पर JVM डेमन थ्रेड के साथ बंद हो जाता है।
Q12. How does the ExecutorService framework improve upon manually creating new Thread(runnable).start() per request?
(प्रति अनुरोध मैन्युअल रूप से new Thread(runnable).start() बनाने की तुलना में ExecutorService कैसे बेहतर है?)
(A) It automatically converts Runnable to Daemon
(B) It reuses a bounded pool of worker threads, avoiding OS thread creation overhead and OOM crashes
(C) It executes all Runnable tasks on the single main thread
(D) It removes the need for the run() method
Correct Answer: (B)
[EN] Spawning unbounded OS threads exhausts stack memory; ExecutorService pools and reuses worker threads efficiently.
[HI] बार-बार नए OS थ्रेड बनाने से मेमोरी समाप्त हो सकती है; ExecutorService थ्रेड पूल में वर्कर थ्रेड्स को पुनः उपयोग करता है।
Q13. What is the valid range of thread priority constants in java.lang.Thread?
(java.lang.Thread में थ्रेड प्रायोरिटी स्थिरांकों की वैध सीमा (रेंज) क्या है?)
(A) -10 to +10, default 0
(B) 1 to 5, default 3
(C) 0 to 100, default 50
(D) 1 (MIN_PRIORITY) to 10 (MAX_PRIORITY), default 5 (NORM_PRIORITY)
Correct Answer: (D)
[EN] Thread defines MIN_PRIORITY = 1, NORM_PRIORITY = 5, and MAX_PRIORITY = 10; invalid values throw IllegalArgumentException.
[HI] Thread में MIN_PRIORITY=1, NORM_PRIORITY=5 और MAX_PRIORITY=10 होता है; सीमा से बाहर मान पर IllegalArgumentException आता है।
Q14. What is the output of: class T extends Thread { public void run() { System.out.print("A"); } } T t = new T(); t.run(); System.out.print("B");
(इस कोड का आउटपुट क्या होगा? class T extends Thread { public void run() { System.out.print("A"); } } T t = new T(); t.run(); System.out.print("B");)
(A) Either AB or BA unpredictably
(B) Only B
(C) Compilation error
(D) AB (always deterministic)
Correct Answer: (D)
[EN] Because t.run() runs synchronously on the main thread before System.out.print("B"), AB is always printed.
[HI] चूंकि t.run() सिंक्रोनस रूप से main थ्रेड पर ही चलता है, इसलिए हमेशा निश्चित रूप से AB प्रिंट होगा।
Q15. If a class implements Runnable and does NOT override public void run(), what happens?
(यदि कोई नॉन-एब्स्ट्रैक्ट क्लास Runnable को इम्प्लीमेंट करे लेकिन public void run() को ओवरराइड न करे, तो क्या होगा?)
(A) Compile-time error because the abstract method run() must be implemented
(B) Runtime AbstractMethodError
(C) Warning only; code compiles
(D) JVM inherits Thread.run() automatically
Correct Answer: (A)
[EN] Any concrete class implementing Runnable must provide a public void run() implementation or be declared abstract.
[HI] कंक्रीट क्लास के लिए Runnable के एब्स्ट्रैक्ट मेथड public void run() को लागू करना अनिवार्य है, वरना कंपाइल एरर आएगा।
Q16. Which of the following statements about Thread and Runnable is NOT true?
(Thread और Runnable के बारे में निम्नलिखित में से कौन सा कथन असत्य (NOT true) है?)
(A) Thread implements the Runnable interface internally
(B) Calling t.start() guarantees the new thread executes before the next line in main()
(C) A single Runnable object can be passed to multiple Thread instances
(D) Thread scheduling order across cores is controlled by the OS/JVM scheduler
Correct Answer: (B)
[EN] t.start() merely puts the thread in RUNNABLE state; execution timing relative to main() is non-deterministic.
[HI] t.start() थ्रेड को केवल RUNNABLE अवस्था में लाता है; यह गारंटी नहीं है कि वह main() की अगली लाइन से पहले चलेगा।
Q17. Within Multithreading: Thread Class vs Runnable Interface, which feature / aspect directly maps to: "Cannot extend any other class (single inheritance)"?
(Multithreading: Thread Class vs Runnable Interface में कौन सा Feature / Aspect सीधे "Cannot extend any other class (single inheritance)" से संबंधित है?)
(A) Design Coupling
(B) Java 8 Lambda & Result
(C) Inheritance Flexibility
(D) Resource Sharing
Correct Answer: (C)
[EN] In Multithreading: Thread Class vs Runnable Interface, 'Inheritance Flexibility' handles 'Cannot extend any other class (single inheritance)' (Can extend another class and implement interfaces).
[HI] Multithreading: Thread Class vs Runnable Interface में 'Inheritance Flexibility' का कार्य/विवरण 'Cannot extend any other class (single inheritance)' (Can extend another class and implement interfaces) है।
Q18. Which option accurately describes 'Design Coupling' in Multithreading: Thread Class vs Runnable Interface?
(Multithreading: Thread Class vs Runnable Interface के संदर्भ में 'Design Coupling' की सही विशेषता कौन सी है?)
(A) Not a functional interface; void run()
(B) Couples task logic with Thread object lifecycle
(C) Cannot extend any other class (single inheritance)
(D) Each thread creates its own separate object
Correct Answer: (B)
[EN] 'Design Coupling' provides 'Couples task logic with Thread object lifecycle' — key point: Separates task from thread execution & Executor pools.
[HI] 'Design Coupling' का मुख्य विवरण 'Couples task logic with Thread object lifecycle' है (Separates task from thread execution & Executor pools)।
Q19. In Multithreading: Thread Class vs Runnable Interface, identify the correct parameter for the specification: "Each thread creates its own separate object".
(Multithreading: Thread Class vs Runnable Interface में "Each thread creates its own separate object" विवरण किस विकल्प के लिए सही है?)
(A) Resource Sharing
(B) Inheritance Flexibility
(C) Java 8 Lambda & Result
(D) Design Coupling
Correct Answer: (A)
[EN] 'Resource Sharing' corresponds to 'Each thread creates its own separate object' (Single Runnable instance can be shared by many threads).
[HI] 'Resource Sharing' का संबंध 'Each thread creates its own separate object' (Single Runnable instance can be shared by many threads) से है।
Q20. Considering Multithreading: Thread Class vs Runnable Interface, what key behavior defines 'Java 8 Lambda & Result'?
(Multithreading: Thread Class vs Runnable Interface में 'Java 8 Lambda & Result' की मुख्य भूमिका या मान क्या है?)
(A) Each thread creates its own separate object
(B) Cannot extend any other class (single inheritance)
(C) Not a functional interface; void run()
(D) Couples task logic with Thread object lifecycle
Correct Answer: (C)
[EN] 'Java 8 Lambda & Result' represents 'Not a functional interface; void run()' (@FunctionalInterface lambda-ready; void run()).
[HI] 'Java 8 Lambda & Result' का अर्थ/मान 'Not a functional interface; void run()' है (@FunctionalInterface lambda-ready; void run())।
Multithreading: Thread Class vs Runnable Interface — Exam FAQs
Q1: Does java.lang.Thread itself implement Runnable?
क्या java.lang.Thread क्लास स्वयं Runnable इंटरफेस को इम्प्लीमेंट करती है?
[EN] Yes, Thread implements Runnable (`public class Thread implements Runnable`). That is why overriding run() works in Thread subclasses too.
[HI] हाँ, Thread क्लास स्वयं Runnable को इम्प्लीमेंट करती है (`public class Thread implements Runnable`), इसलिए Thread की सबक्लास में run() ओवरराइड होता है।
Q2: Can we pass both a subclassed Thread and a Runnable to Thread constructor?
क्या Thread का ऑब्जेक्ट Runnable के रूप में दूसरे Thread को पास किया जा सकता है?
[EN] Yes, because Thread implements Runnable, passing a Thread instance as a Runnable target compiles, though it is poor design and confusing—always pass a clean Runnable or Callable instead.
[HI] हाँ, तकनीकी रूप से Thread खुद Runnable है, लेकिन यह खराब डिज़ाइन है; कार्य के लिए हमेशा अलग Runnable या Callable पास करना चाहिए।
Q3: Is thread priority guaranteed across all operating systems?
क्या जावा में सेट की गई थ्रेड प्रायोरिटी सभी ऑपरेटिंग सिस्टम पर सख्ती से लागू होती है?
[EN] No. Thread priorities are only hints to the underlying OS thread scheduler, which maps Java's 1–10 scale differently across Windows, Linux, and macOS.
[HI] नहीं। थ्रेड प्रायोरिटी केवल OS शेड्यूलर के लिए एक सुझाव (Hint) है और Windows व Linux पर इसका व्यवहार भिन्न हो सकता है।
Post a Comment
Post a Comment