1. What is a thread in Java?
Answer: In Java, a thread is the smallest unit of execution. It represents a separate path of execution within a program. Multiple threads can run concurrently, enabling multitasking in a Java program.
2. How can you create a thread in Java?
Answer: You can create a thread in Java by either:
- Extending the Thread class and overriding its
run()
method. - Implementing the Runnable interface and passing it to a Thread object.
3. What is the difference between extending Thread and implementing Runnable for creating threads?
Answer:
- Extending Thread: If you extend the
Thread
class, you cannot extend any other class (because Java supports only single inheritance). - Implementing Runnable: When you implement the
Runnable
interface, you can still extend other classes. Implementing Runnable
is generally preferred because it allows better design flexibility and decouples the task from the thread.
4. How can you start a thread in Java?
Answer: To start a thread in Java, you call the start()
method on the thread instance. This method internally invokes the run()
method in a new thread of execution.
5. Explain the synchronized keyword in Java.
Answer: The synchronized
keyword in Java is used to control access to critical sections of code, preventing multiple threads from executing that code simultaneously. It ensures that only one thread can execute the synchronized block or method at a time, preventing data inconsistencies.
6. What is the wait() and notify() method in Java?
Answer:
- wait(): This method causes the current thread to wait until another thread calls the
notify()
or notifyAll()
method for the same object. - notify(): This method wakes up one thread that is waiting on the object's monitor.
- notifyAll(): This method wakes up all threads waiting on the object's monitor.
These methods are crucial for inter-thread communication and synchronization between threads.
7. What is the join() method in Java threads?
Answer: The join()
method is used to wait for a thread to die. It makes the current thread wait until the specified thread has completed its execution. This is useful for thread synchronization and ensuring that certain tasks are completed before others start.
8. What is the Thread Pool in Java?
Answer: A thread pool is a collection of worker threads that are reused for executing tasks. It helps manage the creation and lifecycle of threads efficiently, improving performance by reusing threads rather than creating a new one for each task. The Executor framework provides a mechanism for managing thread pools in Java.
9. Explain the concept of deadlock in Java threads.
Answer: Deadlock occurs when two or more threads are blocked forever, each waiting for the other to release a lock. This situation arises when multiple threads try to lock resources in a circular dependency. It can cause a program to freeze if not handled properly.
10. What is the volatile keyword in Java?
Answer: The volatile
keyword in Java is used to indicate that a variable's value may be changed by multiple threads simultaneously. It ensures that any thread accessing the variable sees the most recent write to that variable, providing visibility across threads.