MultiThreading topic is very important feature of Java Programming language. This post is dedicated to all Java Developers. On this topic many questions will ask in all the interviews where the companies working on banking solutions,Big data processing and async functionality pretty heavily often use multithreding . I had already discussed Multithreading in java with example. You should have awareness of multithreading in java and also life cycle of thread in java.
1. What is Thread in java?
Ans: Thread is an Independent sequential path of execution within a process. Threads are light weight process. Threads are exists in common memory space and can share resources data and code.Threads can run parallel. In java, Thread is represented and controlled by an object of class Java.lang.Thread.
2. What is difference between Thread and Process?
Ans: A Thread is lightweight compared to process.Threads are subdivision of process.Threads exists within a process and shares the process resources like memory whereas Process is self contained execution environment. Each process has its own memory space.
Threads have direct access to the data segment of its process whereas process has own copy of the data segment.
Context-switching between threads in the same process is very fast whereas context-switching between process is slower.
Threads can easily communicate within the process whereas process communicate only system provided inter process communication mechanism.
3. How many ways you can create Thread in java?
Ans: You can create a Thread in two ways in java. They are
a) Extending a Thread class
b) Implementing a Runnable interface
For more details visit: multithreading in java
4. What is Difference between Wait() and sleep() in java?
Ans: Firstly you must aware of where these methods are exists. wait() method exist in Object class whereas sleep() method available in the Thread class. sleep() method is used the currently executing thread to sleep for specific period of time(in milliseconds) whereas wait() method causes the current thread to wait until another thread invokes notify() or notifyall() for this object. Here thread releases the lock as soon as wait is called,but in case of sleep() method the Thread does not release the lock.
5. What is demon Thread in java?
Ans: demon threads are runs in the background. These threads are service provider threads. Garbage collector is the best example for demon threads,because it is used to reclaim the unused memory.
We can make a thread demon by using setDaemon(boolean status) which is available in Thread class.
Ex: public void setDaemon(boolean status)
If you want check a thread is daemon or not:
public boolean isDaemon()
6) What deadlock in java? how to prevent it?
Ans: This is very important interview question.Interviewer want to test your capability how to create and detecting deadlock . Here is the link deadlock in java with example .
7) What is the use of Synchronized in java?
Ans: Synchronized keyword is used in java to control the access of multiple threads on shared resources. Synchronized keyword can be applied to static and instance methods and synchronized block in java of the code. If you apply synchronized keyword for method only one thread can access the same thread then other threads have to wait for the execution of method by one thread. Synchronized keyword provides a lock on the object.
TO KNOW MORE DETAILS : Synchronized block in java
8) What is the internal process when start() method is called?
Ans: A new Thread of execution with a new call stack starts. And then the state of thread change from new to Runnable. When thread gets chance to execute its target run() method start to run.
9) What will happen if you do not override run()method?
Ans: This is basic question on thread , interviewer want to know your knowledge on thread api and you really have knowledge how to create and run a thread.
When we call start() method it internally calls run() method with newly created thread.So if we do not override run() method a newly created thread will not called and nothing happen. that run() method executes just like normal method.
10) What is difference between notify() and notifyall() method in java?
Ans: Their might be several threads waiting for this object and only one of them is chosen.notify() method is main work is wake up a single thread that is waiting on this object whereas notifyall() methods wakes up all the threads that are waiting on this object's monitor.
1. What is Thread in java?
Ans: Thread is an Independent sequential path of execution within a process. Threads are light weight process. Threads are exists in common memory space and can share resources data and code.Threads can run parallel. In java, Thread is represented and controlled by an object of class Java.lang.Thread.
2. What is difference between Thread and Process?
Ans: A Thread is lightweight compared to process.Threads are subdivision of process.Threads exists within a process and shares the process resources like memory whereas Process is self contained execution environment. Each process has its own memory space.
Threads have direct access to the data segment of its process whereas process has own copy of the data segment.
Context-switching between threads in the same process is very fast whereas context-switching between process is slower.
Threads can easily communicate within the process whereas process communicate only system provided inter process communication mechanism.
3. How many ways you can create Thread in java?
Ans: You can create a Thread in two ways in java. They are
a) Extending a Thread class
b) Implementing a Runnable interface
For more details visit: multithreading in java
4. What is Difference between Wait() and sleep() in java?
Ans: Firstly you must aware of where these methods are exists. wait() method exist in Object class whereas sleep() method available in the Thread class. sleep() method is used the currently executing thread to sleep for specific period of time(in milliseconds) whereas wait() method causes the current thread to wait until another thread invokes notify() or notifyall() for this object. Here thread releases the lock as soon as wait is called,but in case of sleep() method the Thread does not release the lock.
5. What is demon Thread in java?
Ans: demon threads are runs in the background. These threads are service provider threads. Garbage collector is the best example for demon threads,because it is used to reclaim the unused memory.
We can make a thread demon by using setDaemon(boolean status) which is available in Thread class.
Ex: public void setDaemon(boolean status)
If you want check a thread is daemon or not:
public boolean isDaemon()
6) What deadlock in java? how to prevent it?
Ans: This is very important interview question.Interviewer want to test your capability how to create and detecting deadlock . Here is the link deadlock in java with example .
7) What is the use of Synchronized in java?
Ans: Synchronized keyword is used in java to control the access of multiple threads on shared resources. Synchronized keyword can be applied to static and instance methods and synchronized block in java of the code. If you apply synchronized keyword for method only one thread can access the same thread then other threads have to wait for the execution of method by one thread. Synchronized keyword provides a lock on the object.
TO KNOW MORE DETAILS : Synchronized block in java
8) What is the internal process when start() method is called?
Ans: A new Thread of execution with a new call stack starts. And then the state of thread change from new to Runnable. When thread gets chance to execute its target run() method start to run.
9) What will happen if you do not override run()method?
Ans: This is basic question on thread , interviewer want to know your knowledge on thread api and you really have knowledge how to create and run a thread.
When we call start() method it internally calls run() method with newly created thread.So if we do not override run() method a newly created thread will not called and nothing happen. that run() method executes just like normal method.
10) What is difference between notify() and notifyall() method in java?
Ans: Their might be several threads waiting for this object and only one of them is chosen.notify() method is main work is wake up a single thread that is waiting on this object whereas notifyall() methods wakes up all the threads that are waiting on this object's monitor.
No comments:
Post a Comment