Java Interview Question Set 15
Categories: Java 8(JDK1.8)
What are the states in the lifecycle of a Thread?
A thread can have one of the following states during its lifetime:
New: In this state, a Thread class object is created using a new operator, but the thread is not alive. Thread doesn't start until we call the start() method.
Runnable: In this state, the thread is ready to run after calling the start() method. However, the thread is not yet selected by the thread scheduler.
Running: In this state, the thread scheduler picks the thread from the ready state, and the thread is running.
Waiting/Blocked: In this state, a thread is not running but still alive, or it is waiting for the other thread to finish.
Dead/Terminated: A thread is in terminated or dead state when the run() method exits.
What is the difference between preemptive scheduling and time slicing?
Under preemptive scheduling, the highest priority task executes until it enters the waiting or dead states or a higher priority task comes into existence. Under time slicing, a task executes for a predefined slice of time and then reenters the pool of ready tasks. The scheduler then determines which task should execute next, based on priority and other factors.
What is context switching?
In Context switching the state of the process (or thread) is stored so that it can be restored and execution can be resumed from the same point later. Context switching enables the multiple processes to share the same CPU.
Differentiate between the Thread class and Runnable interface for creating a Thread?
The Thread can be created by using two ways.
a) By extending the Thread class
b) By implementing the Runnable interface
However, the primary differences between both the ways are given below:
a) By extending the Thread class, we cannot extend any other class, as Java does not allow multiple inheritances while implementing the Runnable interface; we can also extend other base class(if required).
b) By extending the Thread class, each of thread creates the unique object and associates with it while implementing the Runnable interface; multiple threads share the same object
c) Thread class provides various inbuilt methods such as getPriority(), isAlive and many more while the Runnable interface provides a single method, i.e., run().
What does join() method?
The join() method waits for a thread to die. In other words, it causes the currently running threads to stop executing until the thread it joins with completes its task. Join method is overloaded in Thread class in the following ways.
a) public void join()throws InterruptedException
b) public void join(long milliseconds)throws InterruptedException
Describe the purpose and working of sleep() method.
The sleep() method in java is used to block a thread for a particular time, which means it pause the execution of a thread for a specific time. There are two methods of doing so.
Syntax:
a) public static void sleep(long milliseconds)throws InterruptedException
b) public static void sleep(long milliseconds, int nanos)throws InterruptedException
What is the difference between wait() and sleep() method?
wait()sleep()
1) The wait() method is defined in Object class.The sleep() method is defined in Thread class.
2) The wait() method releases the lock.The sleep() method doesn't release the lock.
Can we call the run() method instead of start()?
Yes, calling run() method directly is valid, but it will not work as a thread instead it will work as a normal object. There will not be context-switching between the threads. When we call the start() method, it internally calls the run() method, which creates a new stack for a thread while directly calling the run() will not create a new stack.
What about the daemon threads?
The daemon threads are the low priority threads that provide the background support and services to the user threads. Daemon thread gets automatically terminated by the JVM if the program remains with the daemon thread only, and all other user threads are ended/died. There are two methods for daemon thread available in the Thread class:
a) public void setDaemon(boolean status): It used to mark the thread daemon thread or a user thread.
b) public boolean isDaemon(): It checks the thread is daemon or not.