There is given a list of multi threading interview questions and answers. If you know any multi threading interview question, kindly post it in the comment section.
Question-100: What is multithreading?
Answer:
- Multithreading is a process of executing multiple threads simultaneously.Its main advantage is: Threads share the same address space.
- Thread is lightweight.
- Cost of communication between process is low.
Question-101: What is thread?
Answer:
A thread is a lightweight subprocess.It is a separate path of execution.It is called separate path of execution because each thread runs in a separate stack frame.
Question-102: What is the difference between preemptive scheduling and time slicing?
Answer:
Under preemptive scheduling, the highest priority task executes until it enters the waiting or dead states or a higher priority task comes into existence. Under time slicing, a task executes for a predefined slice of time and then reenters the pool of ready tasks. The scheduler then determines which task should execute next, based on priority and other factors.
Question-103: What does join() method?
Answer:
The join() method waits for a thread to die. In other words, it causes the currently running threads to stop executing until the thread it joins with completes its task.
Question-104: What is difference between wait() and sleep() method?
Answer:
- The wait() method is defined in Object class. The sleep() method is defined in Thread class.
- wait() method releases the lock. The sleep() method doesn’t releases the lock.
Question-105: Is it possible to start a thread twice?
Answer:
No, there is no possibility to start a thread twice. If we does, it throws an exception.
Question-106: Can we call the run() method instead of start()?
Answer:
yes, but it will not work as a thread rather it will work as a normal object so there will not be context-switching between the threads.
Question-107: What about the daemon threads?
Answer:
The daemon threads are basically the low priority threads that provides the background support to the user threads. It provides services to the user threads.
Question-108: Can we make the user thread as daemon thread if thread is started?
Answer:
No, if you do so, it will throw IllegalThreadStateException .
Question-109: What is shutdown hook?
Answer:
The shutdown hook is basically a thread i.e. invoked implicitly before JVM shuts down. So we can use it perform clean up resource.
Question-110: When should we interrupt a thread?
Answer:
We should interrupt a thread if we want to break out the sleep or wait state of a thread.
Core Java : Synchronization Interview Questions
The following interview questions are also the part of multi threading interview questions.
Question-111: What is synchronization?
Answer:
Synchronization is the capabilility of control the access of multiple threads to any shared resource.It is used:
To prevent thread interference.
To prevent consistency problem.
Question-112: What is the purpose of Synchronized block?
Answer:
Synchronized block is used to lock an object for any shared resource.
Scope of synchronized block is smaller than the method.
Question-113: Can Java object be locked down for exclusive use by a given thread?
Answer:
Yes. You can lock an object by putting it in a “synchronized” block. The locked object is inaccessible to any thread other than the one that explicitly claimed it.
Question-114: What is static synchronization?
Answer:
If you make any static method as synchronized, the lock will be on the class not on object.
Question-115:What is the difference between notify() and notifyAll()?
Answer:
notify() is used to unblock one waiting thread whereas notifyAll() method is used to unblock all the threads in waiting state.
Question-116: What is deadlock?
Answer:
Deadlock is a situation when two threads are waiting on each other to release a resource. Each thread waiting for a resource which is held by the other waiting thread.
No comments:
Post a Comment