Tuesday 1 May 2012

JAVA interview questions 32

Q311. What is race condition? 
Ans. Multiple Threads are accessing simultaneously and causing data inconsistency problem is called race condition, we can resolve this by using synchronized keyword. 

Q312. What is Daemon Thread? And give an example? 
Ans. The Threads which are running in the background are called Daemon Thread. 
Example: Garbage collector. 

Q313. What is the purpose of a Daemon Thread? 
Ans. The main purpose of Daemon Threads is to provide support for non-daemon Threads. 

Q314. How we can check Daemon nature of a Thread? 
Ans. We can check Daemon nature of a Thread by using isDaemon() method. 

Q315. Is it possible to change a Daemon nature of a Thread? 
Ans. Yes, we can change Daemon nature of a Thread by using setDaemon() method. 

Q316. Is main thread is Daemon or non-daemon? 
Ans. By default main thread is always non-daemon nature. 

Q317. Once we created a new thread is it daemon or non-daemon. 
Ans. Once we created a new Thread, The Daemon nature will be inheriting from parent to child. If the parent is Daemon the child is also Daemon and if the parent is non-daemon then child is also non-daemon. 

Q318. After starting a thread is it possible to change Daemon nature? 
Ans. We can change the Daemon nature before starting the Thread only. Once Thread started we are not allow to change Daemon nature otherwise we will get RuntimeException sying IllegalThreadStateException. 

Q319. When the Daemon thread will be terminated? 
Ans. Once last non-daemon Thread terminates automatically every Daemon Thread will be terminated. 

Q320. What is green Thread? 
Ans. A green thread refers to a mode of operation for the Java Virtual Machine (JVM) in which all code is executed in a single operating system thread. If the Java program has any concurrent 
threads, the JVM manages multi-threading internally rather than using other operating system threads. 
There is a significant processing overhead for the JVM to keep track of thread states and swap between them, so green thread mode has been deprecated and removed from more recent Java implementations.

No comments:

Post a Comment