Các câu hỏi phỏng vấn và câu trả lời về phần Multithreading
1) What is multithreading?
Multithreading is a process of executing multiple threads simultaneously. Multithreading is used to obtain the multitasking. It consumes less memory and gives the fast and efficient performance. Its main advantages are:
-
Threads share the same address space.
-
The thread is lightweight.
-
The cost of communication between the processes is low.
More details.
2) What is the thread?
A thread is a lightweight subprocess. It is a separate path of execution because each thread runs in a different stack frame. A process may contain multiple threads. Threads share the process resources, but still, they execute independently.
More details.
3) Differentiate between process and thread?
There are the following differences between the process and thread.
-
A Program in the execution is called the process whereas; A thread is a subset of the process
-
Processes are independent whereas threads are the subset of process.
-
Process have different address space in memory, while threads contain a shared address space.
-
Context switching can be faster between the threads as compared to context switching between the threads.
-
Inter-process communication is slower and expensive than inter-thread communication.
-
Any change in Parent process doesn't affect the child process whereas changes in parent thread can affect the child thread.
-
4) What do you understand by inter-thread communication?
5) What is the purpose of wait() method in Java?
The wait() method is provided by the Object class in Java. This method is used for inter-thread communication in Java. The java.lang.Object.wait() is used to pause the current thread, and wait until another thread does not call the notify() or notifyAll() method. Its syntax is given below.
public final void wait()
6) Why must wait() method be called from the synchronized block?
We must call the wait method otherwise it will throw java.lang.IllegalMonitorStateException exception. Moreover, we need wait() method for inter-thread communication with notify() and notifyAll(). Therefore It must be present in the synchronized block for the proper and correct communication.
7) What are the advantages of multithreading?
Multithreading programming has the following advantages:
8) What are the states in the lifecycle of a Thread?
A thread can have one of the following states during its lifetime:
-
The process of communication between synchronized threads is termed as inter-thread communication.
-
Inter-thread communication is used to avoid thread polling in Java.
-
The thread is paused running in its critical section, and another thread is allowed to enter (or lock) in the same critical section to be executed.
-
It can be obtained by wait(), notify(), and notifyAll() methods.
-
Multithreading allows an application/program to be always reactive for input, even already running with some background tasks
-
Multithreading allows the faster execution of tasks, as threads execute independently.
-
Multithreading provides better utilization of cache memory as threads share the common memory resources.
-
Multithreading reduces the number of the required server as one server can execute multiple threads at a time.
-
New: In this state, a Thread class object is created using a new operator, but the thread is not alive. Thread doesn't start until we call the start() method.
-
Runnable: In this state, the thread is ready to run after calling the start() method. However, the thread is not yet selected by the thread scheduler.
-
Running: In this state, the thread scheduler picks the thread from the ready state, and the thread is running.
-
Waiting/Blocked: In this state, a thread is not running but still alive, or it is waiting for the other thread to finish.
-
Dead/Terminated: A thread is in terminated or dead state when the run() method exits.
Người sưu tầm: Nguyễn Thị Bảo Trang