Java Interview Questions - Multithreading
1. What is Thread from java point of view?
Thread is a light weight sub process which is suitable for game & network application. It is a separate path of execution.
2. Difference between Process and Thread in java?
The difference between Process and thread is the running program of computer is known as Process where Thread is a light weight sub process, a smallest unit of processing. There can be multiple process inside the OS and one process can have multiple threads.
3. What is the name of default Thread in java?
main is the default thread in java.
4. Difference between void sleep () and void suspend () method in java?
Sleep() method block the thread for a specific period of time Where as suspend() method block the thread for a unknown period of time.
5. Difference between wait () and sleep () in java?
The difference between wait() and sleep() method is wait() method cause thread to release the lock and wait until either another thread invoked the notify() or notifyAll() method for this object Where as sleep() method block the thread for a specific period of time.
6. What are the ways in which a thread can enter the waiting state?
Thread can enter the waiting state by 3 different ways:
- A Thread can enter the waiting state by invoking its sleep () method.
- By blocking on I/O by unsuccessfully attempting to acquire an object’s lock or by invoking an object’s wait () method.
- It can also enter the waiting state by invoking it’s suspend () method.
7. How can a dead thread be restarted? Ans:
A dead thread cannot be restarted.
8. Explain isAlive() method under Thread class?
Boolean isAlive() method check the thread is alive or dead.
9. Which method must be implemented by all threads?
Run() must be implemented by all threads.
10. Why to use multithreading than multiprocessing?
Threads share a common memory area so they save memory .switching between the threads takes less time than process. So multithreading is more used then multiprocessing.
11. How thread create in java?
Thread able to create in java by 2 ways
- By extending thread class.
- By implementing Runnable interface.
12. Define multithreading?
When a process will divide into multiple path and each path able to execute independently then it is known as Multithreading. In other words Multithreading is a process of executing multiple threads simultaneously.
13. Define Multitasking?
Multitasking is a process of executing multiple tasks simultaneously.
14. How multitasking can be achived?
Multitasking can be achieved by 2 ways:
- Process based Multitasking (Multiprocessing).
- Thread based Multitasking (Multithreading).
15. What are the diff. b/w multithreading and multiprocessing?
- Multithreading is the one in which there exist multiple flow of controls whereas it is the one in which there exist single flow of control.
- Threads share the same address space where as Each thread have its own address in memory.
- cost of communication between the threads are low where as s cost of communication between the process is high.
- Thread is light weight. process is heavy weight.
16. State the work of Newborn State, Active state, Block state and Dead state? OR Describe the life cycle of Thread?
New Born State - In this state thread may enter into Dead or Active state. Active State - In this state thread actually execute. Block State - In this state thread wait for some resource. Dead State - In this state thread de allocates the memory.
17. Which method return object of thread class?
Static thread currentThread() method retuens the object of Thread class.
18. State the work of void run() method?
It is used to perform action for a thread.
19. What is the diff. b/w StringgetName() and void setName() method?
String getName() method returns the name of Thread where as Void setName() method assign a name to thread.
20. What is the diff. b/w intgetPriority() and intsetPriority() method?
getPriority () method return the priority of the thread where as setPriority() method change the priority of thread.
21. What is the diff.b/w Boolean isInterrupted() and Static isInterrupted() method?
Boolean isInterrupted() method test if the thread bas been intertupted where as static Boolean isInterruptedmethod() test if the current thread has been interrupted.
22. State MAX_PRIORITY,MIN_PRIORITY,NORM_PRIORITY of thread?
The maximum of priority of thread is MAX_PRIORITY which value is 10. The maximum of priority of thread is NORM_PRIORITY which value is 5. The maximum of priority of thread is NORM_PRIORITY which value is 1.
23. What is the difference between extending thread class and implementing Runnable interface?
One difference between implementing Runnable and extending Thread is that by extending Thread, each of your threads has a unique object associated with it, whereas implementing Runnable, many threads can share the same object instance.
24. Which is better for thread extending thread class or implementing runnable interface OR Which way is better to create a Thread and Why?
When there is a need to extend a superclass, implementing the Runnable interface is more appropriate than using extending Thread class. Because we can extend another class while implementing Runnable interface to make a thread. But if we just extend the Thread class we can’t inherit from any other class.
25. How many thread execute at a single time?
Only one thread can execute at a single time.
26. Who control lifecycle of thread?
JVM control life cycle of Thread.
27. How many method present in Runnable interface & state the use of the methods?
Runnable interface have only one method i.erun() method.it is used to perform action for a thread.
28. What is the work of Start() method of thread class?
Start() method make the thread eligible to run.
29. How to perform multiple task by multithreads?
If you want to perform multiple tasks by multi threads then have to use multiple run method.
30. How to perform single task by multithreads?
If you want to perform single task by multi threads then have to use only one run() method.
31. State the situation where multiclass object is Thread object?
Thread class constructor allocate new thread object ,when multiclass object create ,class constructor is invoked from where Thread class constructor is invoked. This is the situation where multiclass object is Thread class.
32. What happened if you are not extending the thread class?
If you are not extending the thread class then your class object would not be treated as aobject.Then you need to create thread class object.
33. What is thread scheduler?
Thread scheduler is the part of the JVM that decide which thread should run.
34. Which scheduling used by thread scheduler to schedule the thread?
Thread scheduler mainly use preemptive or time slicing scheduling to schedule the thread.
35. What is the diff. b/w Preemptive scheduling and time slicing?
Under preemptive scheduling the highest priority task execute until it enter the waiting or dead state or highest priority task comes into existence. Under time slicing a task execute for a predefined slice of time and then re-enter the pool of ready tasks.
36. Which two methods provides by the thread class for sleeping a thread?
The two methods are:
- public static void sleep (long milisecond)throws InteruptedException.
- public static void sleep(long millisecond ,int names) throws InteruptedException.
37. Can we start a thread twice and mention why?
No we cannot start a thread twice because once a thread start it never be started again because if we do so then an IlligalThreadStateException is thrown.
38. What If we call run() method directly instead of start() method?
Each thread starts in a separate call stack. Invoking the run() method from main thread, the run() method goes into the current call stack rather than at the beginning of a new stack.
39. What is the work of join() method?
The join() method wait for a thread to die.in other word it causes the currently running thread to stop executing until the thread join with complete its task.
40. State the work of CurrentThread() method?
The currentThread() method returns a reference to the currently executing thread object.
41. What is Preemptive Scheduling?
Thread scheduler schedules the threads according to their priority which is known as preemptive scheduling.
42. Which is a low priority Thread?
Daemon Thread is a low priority thread.
43. What is Daemon thread?
Daemon thread in java are those thread which runs in background and mostly created by JVM for performing background task like garbage collection and other housekeeping task.
44. What is the difference between daemon thread and user thread?
Main difference between both is as soon as all user thread finish execution java program or JVM terminates itself, JVM doesn’t wait for daemon thread to finish their execution.As soon as last non daemon thread finished JVM terminates no matter how many daemon thread exists or running inside JVM.
45. What are the methods present in java for Daemon Thread?
java. lang .Thread class provides two methods for Daemon thread. public void setDaemon(Boolean status). public Boolean isDaemon ().
46. What is the diff.b/w void setDaemon() and Boolean isDaemon() methods?
setDaemon (Boolean status) used to mark the current thread as Daemon thread or user thread where Boolean isDaemon() used to check that current is daemon.
47. What to do if you want to make a user thread to Daemon?
If you want to make a user thread as Daemon ,it must not be started otherwise throw IlligalThreadStateException Daemon.
48. What is the use of TheShutdown Hook?
The Shutdown Hook can be used to perform clean up resource or save the state when JVM shut down normally or abruptly.
49. When JVM Shutdown?
The JVM shut down in following situations.
- user logoff
- user shutdown
- System.exit (int)method invoked
- user press ctrl+c on the command prompt.
50. State the use of addShutdownHook() method?
The addShutdown Hook () method of Runnable class is used to register the thread with the virtual machine.
51. Define Synchronization & its type?
Synchronization is the capability of control the access of multiple threads to any shared resource. It is of 2 types:
- process synchronization.
- Thread synchronization.
52. State use of Synchronization?
Synchronization mainly used in case of
- To prevent thread interference.
- To prevent consistency problem.
53. Define types of thread Synchronization?
Thread synchronization are of two types:
- Mutual exclusive
- Inter thread communication.
54. How The Shutdown sequence can stop?
The shutdown sequence can be stopped by invoking the halt(int) method of Runtime class.
55. Define concept of Lock?
Synchronization is built around an internal entity known as Lock.
56. What is the problem without Synchronization?
If there is no Synchronization ,then output is inconsistent.
57. State Synchronized methods & its use?
If you declare any method as synchronized then it is known as synchronized method .it is used to lock an object for any shared resource.
58. State the use of Synchronized Block?
Synchronized block used to perform synchronization on any specific resource of the method.
59. When Synchronized block work same as Synchronized method?
If we put all the codes of the method in the synchronized block then it will work same as the synchronized method.
60. What is Deadlock?
When thread are waiting for each other to release the lock,the condition is called Deadlock.
61. What is Cooperation or Inter-thread communication?
Cooperation is all about making synchronized threads communicate with each other.
62. By which method Cooperation can implemented?
Cooperation can be implemented by:
63. What is the diff. b/w notify() and notifyAll() methods?
notify() use to wake up a single thread that is waiting for this objects monitor. Where notifyAll() use to wake up all threads that are waiting on this objects monitor.
64. What Wait() method do?
wait() method cause thread to release the lock and wait until either another thread invoked the notify() or notifyAll() method for this object.
65. What is the concept means “Interrepting a Thread”?
66. What are the methods provide by thread class forinturrepting a Thread?
Three method provided by the Thread class for inturrepting a thread:
- void interrupt()
- static Boolean interrupted()
67. What are the state for Thread present in java?
java provides 5 states. They are:
- Halted or Dead state.
68. What is Halted state?
In this state the thread completed its total execution.
69. State the ways to create the osbject of Thread class?
The object of thread class can be created by 3 ways:
- using factory method.
- using sub class of thread class.
70. How a programmer construct the object of Runtime class?
If programmer want to construct the object of runtime class,the programmer have to call a static method of the Runtime class.
71. Define System class?
System class is a predefined class present in default package which holds some predefined static method and variables.
72. State the methods present in System class &Define each?
- Public static void exit(int)-It halts the process.
- Public static void gc()-To call the garbage collector explicitly.
- Public static void loadLibrary()-Native method use.
- Public static java.lang.StringgetPriority()-This method returns the property allocated with the string argument.
73. State the work of static java.lang.StringgetProperty() method?
This method returns the property allocated with the string argument.
74. State the work of static long currentTimeMillis() method?
It returns the current time in millisecond.
75. Define Process class?
It is a predefined class present in default package. Developer occasionally needs to write the application in some editor where as they want to run it some other editor as it cannot be instantiated.