21. What is a runnable object?
Ans: In Java, each task is an object of the Runnable interface, also known as runnable object.
22. Which method calls the run() method?
Ans: start() method.
23. How a thread is executed in Java?
24. What is the difference between start() and run() method of Thread class?
Ans: start() method is used to start a newly created thread. It internally calls run() method for execution of statements inside run() method because there is a difference between calling the run() method directly and though start() method.
When you will call run() method directly, it will be treated as a normal method by JVM and called in the same thread. No new thread will be stared.
25. What are the states of a thread in Java? or what are the thread states in Java?
or What are the different stages of Thread lifecycle?
Ans: There are five states of thread in Java. They are:
- New: When a thread is started, it goes to New state.
- Runnable: When run() method is called, the thread enters into runnable state and thread is ready for execution of a particular task.
- Running: When the thread executes a particular task, it is in running state.
- Wating/Blocking state: When a thread is made to wait for a certain period of time, it goes to the waiting state.
- Dead: When a thread completes the execution of statements inside run() method, it exits from run() method and terminated or dead.
26. What does start() method of Thread class do?
Ans: The Thread class start() method puts the thread into ready state (runnable state) and makes the thread eligible to run. It automatically calls the run() method.
27. What are the various methods of Thread class used to manage threads?
Ans: The important methods provided by Thread class are as follows:
a) start(): Initiate thread and calls run() method internally.
b) run(): Task to be performed is declared within a run() method.
c) sleep(): It makes the running process to wait for a particular period of time.
d) yield(): Pauses the execution of current thread and allows another thread of equal or higher priority that are waiting to execute.
e) stop(): Stop the execution of thread permanently.
f) join(): Make a thread wait for another thread to terminate its process.
g) isAlive(): Check the thread is alive or not. It returns a boolean value (true or false) that indicates thread is running or not.
h) setPriority(): Set the priority of thread.
28. What is the difference between sleep() and yield() method?
Ans: When sleep() method is called on a thread, the thread will return to its waiting state. When yield() method is called on a thread, the thread returns to the runnable state (ready state).
29. What is the fundamental difference between sleep() and wait() methods?
Ans: The main difference between sleep() and wait() methods is as follows:
a) The wait() method is an Object class method, whereas sleep() is a static method provided by Thread class.
b) wait() method is called on objects whereas, sleep() method is called on threads, not objects.
c) A waiting thread can be waked up by another thread by calling notify() method on the monitor which is being waited on. But a
sleeping thread cannot be woken up.
d) When we call wait() method, the current thread releases the monitor or lock and goes from running state to waiting state can return to runnable state only when notify() or notifyAll() method is invoked on that object.
In the case of sleep(), the current thread does not release the lock. It just sleeps for some pre-defined time period and returns to runnable state when sleep time is up
e) sleep() method does not require any object lock whereas, wait() method needs object lock before it is called.
f) If wait() method is invoked without getting object lock, IllegalMonitorStateException is thrown at runtime. But sleep() method never throws such an exception.
g) wait() method must be called from synchronized block whereas sleep() method can also be called from outside synchronized block.
30. What is thread scheduler in Java?
Ans: Thread scheduler in Java is the component of JVM that determines the execution order of multiple threads on a single processor (CPU). It decides the order in which threads should run. This process is called thread scheduling.
31. What is time-slicing in Java?
Ans: The process of allocating time to threads is known as time slicing in Java. Time-slicing is based on non-priority scheduling.
32. What is thread priority? How do you set a priority for a thread?
Ans: Thread priority in Java is a number assigned to a thread that is used by Thread scheduler to decide which thread should be allowed to execute. It is represented by a number from 1 to 10.
The thread with the highest priority is selected by the scheduler to be executed first.
We set the priority of a thread using the setPriority() method of Thread class. This method accepts an integer value as an argument and sets that value as priority of a thread through which it is called.
33. What is the default priority of a thread in Java?
Ans: The default priority of a thread is 5 (NORM_PRIORITY).
34. What are the different priorities that can be set on a Thread in Java?
Ans: Thread class provides three priorities that can be set on a Thread object in Java. They are as:
a) MIN_PRIORITY: It is the minimum priority that can be assigned to a thread. The value of MIN_PRIORITY is 1.
b) NORM_PRIORITY: It is the default priority that a thread can have. The default priority of a thread is 5.
c) MAX_PRIORITY: This is the maximum priority that is assigned to a thread. The value of MAX_PRIORITY is 10.
35. How to get priority of current thread in Java?
Ans: We can get priority of current thread by using getPriority() method of Thread class. The getPriority() method returns the priority of a thread through which it is called.
36. How to stop a thread in Java?
Ans: Java provided some control methods such as stop(), suspend(), and resume() in JDK 1.0. But these methods deprecated in later releases due to potential deadlock threats.
We know that thread stops automatically as soon as they finish the execution of run() method.
To manually stop, there are two ways through which we can easily stop a thread in java program. They are:
- By using boolean variable.
- By using isInterrupted() method
37. Can a thread is again alive when it goes into the dead state?
Ans: No, once a thread is terminated or moves into dead state, it cannot alive again. If we try to it by calling start() method, we will get an IllegalThreadStateException exception.
38. Can we call run() instead of start() method on a thread in Java?
Ans: Yes. We can call but it will not work as a separate thread. It will just work as a normal object in main thread. So, there will not be context switching between threads.
39. Is it possible to start a thread two times in Java?
Ans: No. There is no possibility to start a thread twice because we can call start() method only once on a thread in Java. If we call it twice, it will throw an exception.
40. In which scenarios can we interrupt a thread in Java?
Ans: In Java, we can interrupt a thread if we want to wake it up from the sleep or wait state.