In this article, I am sharing some essential questions and answers about multithreading and multiprocessing that are particularly useful for freshers preparing for interviews. Understanding these concepts is crucial for anyone looking to enter the field of software development, as they form the backbone of efficient program execution and resource management.
1. What is multitasking?
Answer: Answer: Multitasking is the process of executing multiple tasks concurrently.
Multitasking can be achieved in two ways:
- Multiprocessing: Running multiple processes simultaneously.
- Multithreading: Running multiple threads within a single process.
2. How can multitasking be achieved?
Answer: Multitasking can be achieved through two methods: multiprocessing and multithreading.
3. What is multiprocessing?
Answer: Multiprocessing is the execution of more than one process simultaneously and is best suited for system-level operations.
4. What are the differences between multiprocessing and multithreading?
| Aspect | Multiprocessing | Multithreading | 
|---|---|---|
| Definition | Executing more than one process simultaneously | Executing more than one thread simultaneously | 
| Unit of Execution | Process | Thread | 
| Memory Utilization | Each process has its own memory space | Threads share the same memory space | 
| Data Sharing | Usually more expensive due to separate memory | Less expensive since threads share memory | 
| Context Switching | More expensive as it involves switching between different memory spaces | Less expensive due to shared memory space | 
| Overhead | Higher overhead due to process isolation | Lower overhead due to shared resources | 
| Inter-process Communication | Typically requires complex mechanisms (e.g., pipes, sockets) | Easier and more efficient through shared variables or objects | 
| Isolation | Processes are isolated from each other | Threads are not isolated; they share data within the process | 
| Fault Tolerance | A failure in one process does not affect others | A failure in one thread can potentially affect others in the same process | 
5. How many thread groups are created by the JVM?
Answer: Two thread groups are created by the JVM: a) System Group b) Main Group
6. How many threads are created by the JVM and what are their names?
Answer: The JVM creates five threads:
a) System Group
- Finalizer
- Attach Listener
- Signal Dispatcher
- Reference Handler
b) main group
1) main thread
7. What does the main thread do first when a Java program is run?
Answer: The main thread checks whether the specified class file is available or not.
8. What happens if the specified class file is not available?
Answer: An error message is displayed.
9. What does the JVM do if the specified class file is available?
Answer: It verifies the class file format using the Byte Code Verifier.
10. What are the threads that run under the main group?
Answer: User-created threads and the main thread itself run under the "main" thread group.
11. What are the thread groups created by the JVM at start-up?
Answer: The JVM creates the "main" thread group and the "system" thread group at start-up.
12. What are the threads that run under the system group?
Answer: Threads like garbage collection, finalization, and various internal JVM threads run under the "system" thread group.
13. What is the main thread?
Answer: The main thread is the initial thread that runs when a Java application starts, executing the main method.
14. What is the finalizer thread?
Answer: The finalizer thread executes the finalize method on objects before they are garbage collected.
15. How can I define child threads in Java?
Answer: You can define child threads by either extending the `Thread ` class or implementing the `Runnable` interface and then passing the `Runnable `to a `Thread` object.
In this article, we learned about the basics of multitasking, the methods to achieve it, and the key differences between multiprocessing and multithreading.
Stay tuned for more articles and tips on Java programming and interview preparation.
