Monday 30 April 2012

JAVA interview questions 29

Q281. 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. 

Q282. 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. 

Q283. 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. 

Q284 .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() 

Q285. 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(); 

Q286.What is purpose of join() method? 
Ans. If a Thread wants to wait until some other Thread completion then we should go for join() method. 
Example: if a Thread t1 execute t2.join() ; then t1 will entered into waiting state until t2 Thread completion. 

Q287. Is join() method is overloaded? 
Ans. Yes join() method is overloaded method. 
Public final void join() throws InterruptedException 
By using this method thread will wait up to another thread completion . 
Public final void join(long ms) throws InterruptedException 
By using this method thread will wail upto sometime what we are passing as a argument in millisecond 
Public final void join(long ms, int ns)throws InterruptedException 
By using this method thread will wait up to sometime what we are passing as a argument in millisecond and nanosecond. 

Q288. What is the purpose of sleep() method? 
Ans. If a Thread don’t want to perform any operation for a particular amount of time then we should go for sleep() method.Whenever we are using sleep() method compulsory we should 
handle InterruptedException either by using try-catch or by using throws keyword otherwise we will get compile time error. 

Q289. What is synchronized keyword? Explain its advantages and disadvantages. 
Ans. Synchronized keyword is applicable for method and blocks only. We can’t use for
variables and classes.
If a method declared as a synchronized then at a time only one Thread is allow to
execute that method on the given object.
The main advantages of synchronized keyword are, we can prevent data inconsistency problems and we can provide Threadsafty. 
But the main limitation of synchronized keyword is it increases waiting time of 
Threads and effect performance of the system. Hence if there is no specific requirement it is not recommended to use synchronized keyword. 

Q290.Where we can use synchronized keyword? 
Ans. Synchronization concept is applicable whenever multiple Threads are operating on the same object simultaneously. But whenever multiple Threads are operating on different objects then there is no impact of synchronization. 

No comments:

Post a Comment