- What is multithreading?
Multithreading is a way by which one or more threads can run at the same time to achieve faster execution of a program.
- What is a thread?
A thread is a separate path of execution in a program. One or more threads combine to form a process.
- How can you create a thread?
Thread is created by overriding the run() method. There are two ways for that -
- By extending Thread class.
- By implementing Runnable interface.
- Explain the life cycle of a thread.
Following are the steps -
- New
- Runnable
- Running
- Waiting/Blocked/Sleep
- Dead
- What is a lock in multithreading?
There can be a block of code or a method which can be used by more than one thread at the same time which sometimes causes issues. To avoid usage of a shared resource by more than one thread at a time, a lock is created on that resource which leads other threads to wait.
- How do we acquire a lock?
We can acquire a lock by using “Synchronized” keyword in method signature or by creating a synchronized block.
- What is a priority thread?
When there are multiple threads waiting in the queue and if there is urgency of executing a particular thread then we can add priority to that thread to be picked up before the other threads.
- What are the different types of thread priorities?
Following are the different types of thread priorities -
- MIN_PRIORITY
- NORM_PRIORITY
- MAX_PRIORITY
- What is the use of yield() method?
yield() method is used to make the current running thread into runnable state.
It makes current thread to sleep for a specific amount of time.
- What is the use of sleep() method?
sleep() method is used to sleep the thread for the specified amount of time.
- What is the use of interrupt() method?
interrupt() method is used to interrupt current thread, it does not stops the thread.
- Explain wait(), notify and notifyAll() methods.
wait() -
- What is a daemon thread?
Daemon thread is a thread which runs in background. A example of it is Garbage Collector.
<<Exception Handling
<<Exception Handling