Part 4
Interview questions on multithreading in java for experienced covered in this post:
What will happen if I am trying to run a thread which is already in
running the state?
- Yes, we can't start already running thread. It will throw
IllegalThreadStateException at runtime - if the thread was already
started.
- For your reference please refer to the below example.
package simplifiedjava.crackedInterview; public class RestartedThreadDemo extends Thread{
@Override
public void
run() { }
public static void
main(String[] args) {
Thread t = new
Thread();
t.start();
t.start();
} }
Output: Exception in thread "main" java.lang.IllegalThreadStateException
at
java.lang.Thread.start(Unknown Source)
at
simplifiedjava.crackedInterview.RestartedThreadDemo.main (RestartedThreadDemo.java:14)
|
Can we convert a normal thread into a daemon thread once the thread
started or not started in both cases?
- Yes, we can convert a normal thread into a daemon thread.
- Daemon thread is a very lightweight thread.
- But, a Normal thread can convert into a daemon thread but it is not
possible after the thread is running state. It is possible before the thread
is running state.
- Normal thread can convert by invoking setDaemon() thread.
How do you share the data between two threads?
- We should declare such variables as static and
volatile.
- Volatile variables are shared across multiple threads. This means that
individual threads won’t cache their copy in the thread-local. But every
object would have its own copy of the variable so threads may cache value
locally.
- We know that static fields are shared across all the objects
of the class, and it belongs to the class and not the individual objects.
But, for static and non-volatile variables also, threads may cache the
variable locally.
What is the difference between notify and notifyAll()?
- Notify() method can be used to notify only one thread at a
time which is waiting in a waiting state.
- NotifyAll() method can be used to notify all threads at a time that is
waiting in a waiting state.
What is the difference between preemptive scheduling and time slicing?
|
Preemptive Scheduling |
Time Slicing |
1. |
Executes
highest priority task executes until it highest priority task comes into
existence or thread enters into waiting state or dead state. |
Task executes
for a predefined slice of time and then reenters into the pool of ready
tasks. The scheduler will decide which task needs to execute first according
to priority. |
What is the difference between interrupted() and isInterrupted() method in java?
|
Interrupted() |
isInterrupted() |
1. |
interrupted()
method is a static method of class thread checks the current thread and clear
the interruption "flag". i.e. a second call to interrupted() will
return false. |
isInterrupted()
method is an instance method; it reports the status of the thread on which it
is invoked. it does not clear the interruption flag. |
2. |
Syntax: if(t.interrupted())
{ System.out.println("inside
interrupted"); } |
Syntax: if(t.isInterrupted())
{ System.out.println("inside
isInterrupted."); } |
Why wait(), notify() and notifyAll() method must called/invoke from
synchronized area? What will happen if I invoked outside synchronized
block?
- Threads uses wait(), notify() and notifyAll() method for inter-thread
communication.
- Thread can get a lock either synchronized method or synchronized block.
Thread must get a lock to call wait(), notify() and notifyAll() method.
- If you invoke these methods outside the synchronized block or method then
you will get IllegalMonitorStateException.
What is context switching?
- Context switching is a technique or method used by the operating system to
switch a process from one state to another to execute its function using
CPUs in the system.
- When context switching performed in the system, it stores the old running
process's status in the form of registers and assigns the CPU to a new
process to execute its tasks.
- A context switching helps to share a single CPU across all processes to
complete its execution and store the system's tasks status.
- Context Switching is the process of storing and restoring of CPU state so
that Thread execution can be resumed from the same point at a later point of
time.
What is blocking queue?
- A Queue that additionally supports operations that wait for the queue to
become non-empty when retrieving an element, and wait for space to become
available in the queue when storing an element.
- A BlockingQueue does not accept null elements.
- Implementations throw NullPointerException on attempts to add, put or offer
a null.
- A BlockingQueue may be capacity bounded.
- At any given time it may have a remaining Capacity beyond which no
additional elements can be put without blocking. A BlockingQueue without any
intrinsic capacity constraints always reports a remaining capacity of
Integer.MAX_VALUE.
- BlockingQueue implementations are designed to be used primarily for
producer-consumer queues, but additionally support the Collection
interface.
- BlockingQueue implementations are thread-safe.
How would you check whether Thread holds a lock of object or not?
- You can identify the lock on the particular object by calling wait() or
notify() method on that object. If the object does not hold the lock, then
it will throw llegalMonitorStateException.
- By calling holdsLock(Object o) method. This will return the boolean value.
- Java interview questions and answers all MNC - Click here
- Basic core java interview questions and answers for freshers - Click here
- Core java interview questions for 3 years experience - Click here
- Core java interview questions and answers for 3-5 years exp - Click here
- Core java interview questions and Answers for 5 - 7 Years exp - Click here
- Basic Java Interview Questions and Answers - Click here
- Java interview questions and answers on oops - Click here
- Java interview questions and answers on Strings - Click here
- Java interview questions on exception handling - Click here
- Interview questions on multithreading in java for experienced - Click here
- Interview questions on serialization in java for experienced - Click here
- Interview questions on inner class in java for experienced - Click here
- Interview questions on Collections in java for experienced - Click here
Thank you techies for visiting this blog. I hope you enjoyed this blog and got more technical knowledge. I have tried to cover all types of questions and provided examples tested on eclipse as much as I can. Guys, please don’t just mug up the questions and answers. Try to clear your concepts with examples. Try to write a code on eclipse once you read the concepts. It will help you to memorize the concepts for a very long time. Simultaneously you will be prepared for interview programs as well. It will help you to survive in the IT sector for a long time. It may be easy to crack an interview but it's really tough to survive in the IT industry with inadequate knowledge and skills. I have collected all the questions from actual interviews attended by my friends, colleagues, college mate and classmate. I have covered frequently asked questions as well as challenging questions. I have included many programs to understand the concept thoroughly. I will try to explain the concept with the help of a real-time program in eclipse. You can share more questions which are not covered in this blog or post. Always welcome your suggestions and queries. I will definitely try to resolve it. Please comment your queries and new set of questions under the comment sections. I will create a new post for those questions. My total experience is 10. Initially I had worked on some support projects and then I moved to java projects. I had worked with many multi-national companies like R-Systems, Tata Consultancy Services, Cybage Softwares. Fortunately, TCS and Cybage has given me an opportunity to take interviews for experienced candidates. I have conducted more than 1000 interviews by now. Mock sessions will be conducted for minimal charges. I will guide you personally on how to crack interviews. All sessions will be online sessions only. My interview book will be provided free of cost if you enroll for personal training. Once you have done practice then I assured you will definitely crack almost all java interviews. I have published my book named with "All MNC Java Interview" which is available on amazon. You can go through it or you can directly contact to me if you are interested to read the book. I will give you special discount. I have covered near about 550 questions and answers with program tested on eclipse and necessary diagram. I have covered interview questions asked in all reputed MNC like TCS, Infosys, Capgemini, Tech Mahindra, Cognizant, City Bank, Barclays, Amdocs, Mindtree etc. My purpose behind this book is, help you to get a job in your dream company. I can understand this is a struggling period for job seekers and freshers. Everybody must have gone from this phase. so never give up. Keep upgrading yourself. I am sure you will get a job in your dream company. All the best!!! Please reach out to me for personal training for interview preparation. You can reach out to me at mncjavainterview@gmail.com. |
0 Comments