1. What is a thread in Java?
Answer:
Explanation:
In Java, a thread is a lightweight process that allows for concurrent execution within a program.
2. How do you start a thread in Java?
Answer:
Explanation:
To start a thread in Java, you create an instance of the Thread class and call its start() method.
3. What does the Runnable interface contain?
Answer:
Explanation:
The Runnable interface in Java contains only one method, run(), which is implemented by classes that intend to execute code on a thread.
4. What is the purpose of the run() method in a thread?
Answer:
Explanation:
The run() method defines the code that is executed when the thread starts running.
5. Which method is used to check if a thread is alive?
Answer:
Explanation:
The isAlive() method in the Thread class is used to check if a thread is still running.
6. Can we call the run() method directly to start a thread?
Answer:
Explanation:
Calling the run() method directly does not start a new thread; it just executes the run() method in the current thread.
7. What is thread priority in Java?
Answer:
Explanation:
Thread priority in Java is a measure that indicates how often a thread should be given time to execute, relative to other threads.
8. What happens when you call the sleep() method on a thread?
Answer:
Explanation:
The sleep() method pauses the execution of the current thread for a specified duration.
9. Can two threads share the same instance of a variable?
Answer:
Explanation:
Threads can share the same instance of a variable, especially if the variable is a member of an object accessed by multiple threads.
10. What is synchronization in the context of Java threads?
Answer:
Explanation:
Synchronization is a mechanism that ensures that only one thread at a time accesses a shared resource or critical section.
11. What keyword is used to synchronize a method or block in Java?
Answer:
Explanation:
The synchronized keyword is used to lock a method or a block so that only one thread can access it at a time.
12. What is a deadlock in multithreading?
Answer:
Explanation:
A deadlock is a situation in multithreading where two or more threads are blocked forever, waiting for each other to release the resources they need.
13. What is the Thread class's join() method used for?
Answer:
Explanation:
The join() method is used in a thread to make the current thread wait until the specified thread completes its execution.
14. Which method is used to yield execution to another thread?
Answer:
Explanation:
The yield() method in the Thread class causes the currently executing thread to pause and allow other threads to execute.
15. What does thread safety mean in Java?
Answer:
Explanation:
Thread safety refers to the protection of data from corruption or inconsistent results when it is accessed by multiple threads simultaneously.
16. What is a daemon thread in Java?
Answer:
Explanation:
Daemon threads in Java are background threads that provide services to user threads. They do not prevent the JVM from exiting when all user threads have finished executing.
17. How do you set a thread as a daemon thread in Java?
Answer:
Explanation:
The setDaemon() method of the Thread class is used to mark a thread as a daemon thread.
18. In Java, what happens when a thread's start() method is called more than once?
Answer:
Explanation:
Calling the start() method more than once on a thread in Java results in an IllegalThreadStateException.
19. What is the initial state of a thread when it is created?
Answer:
Explanation:
When a thread is created in Java, its initial state is 'New'. It has not yet started running.
20. What is the purpose of the interrupt() method in Java threads?
Answer:
Explanation:
The interrupt() method is used to send an interrupt signal to a thread, which can be used to request the thread to stop its execution.
21. How can you check if a thread has been interrupted?
Answer:
Explanation:
The isInterrupted() method in the Thread class is used to check if a thread has been interrupted.
22. Can you access local variables of a method in a thread's run() method?
Answer:
Explanation:
Local variables of a method can be accessed in a thread's run() method only if they are declared final.
23. What is the difference between the wait() and sleep() methods in Java threads?
Answer:
Explanation:
The wait() method releases the monitor's lock held by the thread, whereas sleep() keeps the monitor's lock until it wakes up or is interrupted.
24. What is the primary use of the notify() and notifyAll() methods in Java threads?
Answer:
Explanation:
The notify() and notifyAll() methods are used to signal one or all threads that are waiting on the object's monitor to resume execution.
25. What does it mean for a method to be thread-safe?
Answer:
Explanation:
A thread-safe method is one that can be safely invoked by multiple threads at the same time without leading to problems such as data corruption or inconsistent results.