Thread Creation
How can you create a thread in Java?
- By extending the Object class.
- By implementing the Collection interface.
- By extending the Thread class or implementing the Runnable interface.
- By using the 'create' keyword.
- By implementing the Cloneable interface.
Thread States
What are the different states a thread can be in during its lifecycle in Java?
- Created, Running, Waiting, Sleeping, Dead
- New, Runnable, Blocked, Waiting, Terminated
- Active, Idle, Blocked, Suspended, Finished
- Born, Alive, Inactive, Destroyed, Gone
- Initialized, Executing, Paused, Resumed, Stopped
Runnable vs Thread
What is the key difference between implementing the Runnable interface and extending the Thread class in Java?
- Extending Thread allows for multiple inheritance.
- Implementing Runnable is faster than extending Thread.
- Implementing Runnable allows a class to extend another class, while extending Thread does not.
- There is no difference; they are interchangeable.
- Extending Thread is preferred for single-threaded applications.
Start Method Purpose
What is the primary purpose of the start() method in the Thread class?
- To initialize the thread's name.
- To execute the run() method in the current thread.
- To allocate memory for the thread.
- To begin the thread's execution in a new thread.
- To check if the thread is alive.
Synchronization Concept
What does synchronization refer to in the context of Java multithreading?
- The process of aligning thread execution speeds.
- The mechanism to control the access of multiple threads to shared resources.
- The method of distributing tasks evenly among threads.
- The technique to monitor thread activity.
- The procedure for merging thread outputs.
Synchronized Keyword
How does the synchronized keyword work in Java?
- It prevents a thread from being interrupted.
- It ensures that only one thread can execute a critical section of code at a time.
- It optimizes the thread's performance.
- It increases the priority of a thread.
- It automatically detects deadlocks.
Deadlock Definition
In Java multithreading, what is a deadlock?
- A state where a thread is permanently blocked.
- A situation where a thread crashes due to an unhandled exception.
- A state where two or more threads are blocked indefinitely, waiting for each other.
- A situation where a thread enters an infinite loop.
- A state where a thread consumes excessive memory.
Thread Communication
How can threads communicate with each other in Java?
- Through direct memory access.
- Through shared variables and synchronization mechanisms like wait, notify, and notifyAll.
- Through network sockets.
- Through external files.
- By sending emails.
Wait, Notify, NotifyAll
What is the purpose of the wait(), notify(), and notifyAll() methods in Java?
- To control thread priority.
- To manage thread pools.
- To enable threads to pause execution and signal each other when conditions change.
- To terminate threads.
- To measure thread execution time.
Thread Safety
What does thread safety mean, and why is it important in Java?
- It means a thread is immune to exceptions; it is important for robust applications.
- It means multiple threads can access shared resources concurrently without causing data corruption or unexpected behavior; it is important for reliable and predictable multithreaded applications.
- It means a thread always terminates gracefully; it is important for system stability.
- It means a thread has the highest priority; it is important for critical tasks.
- It means a thread consumes minimal resources; it is important for efficient resource utilization.