Monday, 30 April 2012

JAVA interview questions 28

Q271. Explain life cycle of a Thread? 
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. 

Q272. What is the importance of Thread class start() method? 
Ans. Start() method present in Thread class performing all low level joining formalities for the newly created thread like registering thread with Thread Scheduler etc and then start() method 
invoking run() method.As the start() method is doing all low level mandatory activities, 
Programmer has to concentrate only on run() method to define the job. Hence, start() method is a big assistant to the programmer.Without executing Thread class start() method there is no chance of starting a new Thread. 

Q273. After starting a Thread if we trying to restart the same thread once again what will happen? 
Ans. After starting a Thread restarting of the same Thread once again is not allowed violation leads to Runtime Exception saying IllegalThreadStateException. 

Q274. Explain Thread class constructors? 
Ans. There are eight constructors are available in Thread class: 
1. Thread t=new Thread(); 
2. Thread t=new Thread(Runnable r); 
3. Thread t=new Thread(String name); 
4.Thread t=new Thread(Runnable r, String name); 
5.Thread t=new Thread(ThreadGroup g, String name); 
6.Thread t=new Thread(ThreadGroup g, Runnable r); 
7.Thread t=new Thread(ThreadGroup g, Runnable r, String name); 
8.Thread t=new Thread(ThreadGroup g, Runnable r, String name, long stacksize); 

Q275. How to get and set name of a Thread? 
Ans. For every Thread in java there is a name. To set and get the name of a Thread we can use the following methods. All methods are final. 
1.Public final void setName(String name); - To set the name of a Thread 
2.Public final String getName(); - To get the name of a Thread. 

Q276. What is the range of Thread priority in java? 
Ans. The valid range of a Thread priority is 1-10. (1 is least priority and 10 is highest priority) 

Q277. Who uses Thread priority? 
Ans. Thread Scheduler uses priorities while allocating CPU. The Thread which is having highest priority will get chance first for execution. 

Q278. What is the default priority of the Thread? 
Ans. The default priority only for the main thread is 5 but for all remaining threads default 
priority will be inheriting from parent to child. Whatever priority parent thread has the same will be inherited to the child thread. 

Q279. Once we created a new Thread what about its priority? 
Ans. Whatever priority parent thread has the same will be inherited to the new child thread. 

Q280. How to get and set priority of a Thread? 
Ans. To get and set priority of a Thread, Thread class defines the following two methods:; 
1. Public final int
2. Public final void setPriority(int priority);

No comments:

Post a Comment