Multi Threading
Multi Threading
Ans. Once we create a Thread object then the Thread is said to be in New/Born state once we call
t.start() method now the Thread will be entered into ready/Runnable state that is Thread is ready to
execute. If Thread Scheduler allocates CPU now the Thread will entered into the Running state and
start execution of run() method. After completing run() method the Thread entered into Dead State.
Q23. If we are trying to set priority of a Thread as 100 what will happen?
Ans. If we are trying to set priority of a Thread as 100 then we will not get any compile time error but
at the runtime we will get Runtime exception IllegalArgumentException. Because the valid range of the
Thread priority is (1-10) only.
Q24. If two threads having same priority then which thread will get chance first for execution?
Ans. If two threads having same priority then which thread will get the chance first for execution
decided by Thread Scheduler. It is the part of JVM and its behavior is vendor dependent and we can’t
expect exact output.
Q25. If two threads having different priority then which thread will get chance first for execution?
Ans. If two threads having different priority then the Thread which is having highest priority will get
chance first for execution.
Q26 .How we can prevent a thread from execution?
Ans. We can prevent a Thread from executin by using the following methods:
1. Yield()
2. Join()
3. Sleep()
Q27. What is yield() method? Explain its purpose?
Ans. yield() method causes the current executing thread to pause execution and give the chance for
waiting thread are same priority. If there is no waiting thread or all the remaining waiting thread have
low priority then the same thread will get chance once again for execution. The Thread which is
yielded when it will get chance once again for execution depends upon mercy of Thread
scheduler.Public static native void yield();
Q35. While a thread executing any synchronized method on the given object is it possible to execute
remaining synchronized method of the same object simultaneously by any other thread?
Ans. No, it is no possible.
Q36. What is the difference between synchronized method and static synchronized method?
Ans. If a Thread wants to execute a synchronized method first it has to get the lock of the object.
Once a Thread got the lock then it is allow to execute any synchronized method on that object.If a
Thread wants to execute static synchronized method that Thread has to get class level lock once a
Thread got class level lock then only it is allow to execute static synchronized method.
Q37. What is the advantage of synchronized block over synchronized method?
Ans. If very few lines of the code required synchronization then declaring entire method as the
synchronized is not recommended. We have to declare those few lines of the code inside synchronized
block. This approach reduces waiting time of the Thread and improves performance of the system.
Q39. How we can declare synchronized block to get class level lock?
Ans. To get the class level lock we can declare synchronized block as follows:
synchronized(Display.class)
{
}
Q40. How two thread will communicate with each other?
Ans. Two Threads will communicate with each other by using wait(), notify(), notifyAll() methods.
Q41.wait(), notify(), notifyAll() method can available in which class?
Ans. These methods are defined in Object class.
Q42.Why wait(), notify(), notifyAll() method defines in object class instead of Thread class?
Ans. These methods are defined in Object class but not in Thread because Threads are calling this
method on the shared object.
Q43.If a waiting thread got notification then it will entered into which state?
Ans. It will entered into another waiting state to get lock.
Q44.In which method threads can release the lock?
Ans. Once a Thread calls wait() method it immediately releases the lock of that object and then entered
into waiting state similarly after calling notify() method Thread releases the lock but may not
immediately. Except these three methods( wait(), notify(), notifyAll() ) method Thread never releases
the lock anywhere else.
Q45. Explain wait(), notify(), notifyAll() method uses.
Ans. Two Threads will communicate with each other by using wait(), notify() or notifyAll() methods.
These methods are defined in Object class but not in Thread because Threads are calling this
method.
Q47. Once a Thread got the notification then which waiting thread will get chance?
Ans. It is depends on the Thread Scheduler.