Part 3
Interview questions on multithreading in java for experienced covered in this post:
What is Synchronization?
-
Synchronization is a technique used in a multi-threading
environment to control the access of shared resources on multiple
threads.
What is the difference between Synchronization Method and Synchronized Block?
|
Synchronization Method |
Synchronized Block |
1. |
A method declared with a Synchronized keyword is called
Synchronized Method. |
Inside a method, if a chunk of code declared inside block with
Synchronized keyword is called a Synchronized block. |
2. |
Performance is low as compared to Block. |
Performance is high as compared to the method. |
What is the volatile keyword in
java?
-
If you declare a variable as volatile, JVM will store those
variables on main memory.
-
We can make sure every time JVM reads the value from the main
memory.
-
Every time JVM writes the value it will write on the main
memory.
-
Volatile keyword ensures that variable will be visible to all
thread and thread can read the value from main memory.
What will happen If I directly override
the run() method instead of the start() method?
-
First of all, when you invoke the start() method on a thread,
New thread will create and start executing parallelly.
-
Run() method will execute as it is in the same thread.
-
Run() method will be treated as a normal overridden method of
the Thread class.
What will happen when an exception
occurred in a thread?
-
Exception behaviour won’t change in the case of non-threading
or multi-threading programming.
-
Once an exception occurs and if it is not handled then your
program will be terminated abruptly.
-
In the case of a multi-threading environment if an exception
occurs then the thread will die.
-
If an uncaught exception handler is registered then it will
get a callback.
-
Nested interface
Thread.UncaughtExceptionHandler
is defined for handlers invoked when a Thread abruptly terminates due to an
uncaught exception.
When a thread is about to terminate due to an uncaught exception the Java
Virtual Machine will query the thread for its UncaughtExceptionHandler
using
Thread.getUncaughtExceptionHandler()
and will invoke the handler's
uncaughtException() method,
passing the thread and the exception as arguments.
What is readwritelock in java?
-
A ReadWriteLock maintains a pair of associated locks.
-
One for read-only operations and one for writing.
-
It allows various threads to read a specific resource but
allows only one to write it at a time.
-
The read lock may be held simultaneously by multiple reader
threads, so long as there are no writers. The write lock is exclusive.
-
All ReadWriteLock implementations must guarantee that the
memory synchronization effects of write-lock operations (as specified in the
Lock interface) also hold with respect to the associated readLock. That is,
a thread successfully acquiring the read lock will see all updates made upon
the previous release of the write lock.
-
public interface
ReadWriteLock {}.
What is Thread-Local class? What is the
purpose to have this class?
-
We can easily create classes whose fields are thread-local by
simply defining private fields in the Thread class.
-
We can create thread-safe classes that don’t share states
between threads by making their fields thread-local.
-
Thread-Local is most commonly used to implement per-request
global variables in app servers or servlet containers where each user or
client request is handled by a separate thread.
-
For example, Spring, Spring Security and JSF each have the
concept of a "Context" through which functionality of the framework is
accessed. And in each case, that context is implemented both via dependency
injection (the clean way) and as a Thread-Local in cases where DI can't or
won't be used.
-
Please refer the below example.
package simplifiedjava.crackedInterview;
import
java.util.Arrays; import java.util.List; public class ThreadLocalDemo1 extends Thread { private final List<Integer> numList = Arrays.asList(10,20,30,40,50);
@Override
public
void
run() {
for(int
n
: numList) {
System.out.println(n);
}
} } package simplifiedjava.crackedInterview;
import
java.util.Arrays; import java.util.List; public class ThreadLocalDemo2 extends Thread{ private final List<String> wordList = Arrays.asList("Hi","Hello","GM","GA","GN");
@Override
public
void
run() {
for(String str
: wordList) {
System.out.println(str);
}
}
} |
What is the ThreadGroup?
-
Thread group represents a set of threads.
-
ThreadGroup belongs to
java.lang.ThreadGroup.
-
A thread is allowed to access information about its own
thread group but not to access information about its thread group's parent
thread group or any other thread group.
-
A thread group can also include the other thread group. The
thread group creates a tree in which every thread group except the initial
thread group has a parent.
What is a Thread pool?
-
Thread pool is a set of threads that resides in a single
place.
What is the difference between volatile
and atomic variable?
-
Volatile Variable: Volatile keyword is used in java synchronization technique to achieve
thread-safety, basically to overcome the visibility problem. The variables
marked as volatile gets stored in main memory rather than CPU cache.
-
Atomic Variable:
Atomic variables are used for performing atomic operations on a single
variable instead of a compounded operation, So it solves the synchronization
problem ( race condition etc.)
- Java interview questions and answers all MNC - Click here
- Basic core java interview questions and answers for freshers - Click here
- Core java interview questions for 3 years experience - Click here
- Core java interview questions and answers for 3-5 years exp - Click here
- Core java interview questions and Answers for 5 - 7 Years exp - Click here
- Basic Java Interview Questions and Answers - Click here
- Java interview questions and answers on oops - Click here
- Java interview questions and answers on Strings - Click here
- Java interview questions on exception handling - Click here
- Interview questions on multithreading in java for experienced - Click here
- Interview questions on serialization in java for experienced - Click here
- Interview questions on inner class in java for experienced - Click here
- Interview questions on Collections in java for experienced - Click here
Thank you techies for visiting this blog. I hope you enjoyed this blog and got more technical knowledge. I have tried to cover all types of questions and provided examples tested on eclipse as much as I can. Guys, please don’t just mug up the questions and answers. Try to clear your concepts with examples. Try to write a code on eclipse once you read the concepts. It will help you to memorize the concepts for a very long time. Simultaneously you will be prepared for interview programs as well. It will help you to survive in the IT sector for a long time. It may be easy to crack an interview but it's really tough to survive in the IT industry with inadequate knowledge and skills. I have collected all the questions from actual interviews attended by my friends, colleagues, college mate and classmate. I have covered frequently asked questions as well as challenging questions. I have included many programs to understand the concept thoroughly. I will try to explain the concept with the help of a real-time program in eclipse. You can share more questions which are not covered in this blog or post. Always welcome your suggestions and queries. I will definitely try to resolve it. Please comment your queries and new set of questions under the comment sections. I will create a new post for those questions.
My total experience is 10. Initially I had worked on some
support projects and then I moved to java projects. I had
worked with many multi-national companies like R-Systems,
Tata Consultancy Services, Cybage
Softwares. Fortunately, TCS and Cybage has given
me an opportunity to take interviews for experienced
candidates. I have conducted more than 1000 interviews by
now. Mock sessions will be conducted for minimal charges. I will guide you personally on how to crack interviews. All sessions will be online sessions only. My interview book will be provided free of cost if you enroll for personal training. Once you have done practice then I assured you will definitely crack almost all java interviews. I have published my book named with "All MNC Java Interview" which is available on amazon. You can go through it or you can directly contact to me if you are interested to read the book. I will give you special discount. I have covered near about 550 questions and answers with program tested on eclipse and necessary diagram. I have covered interview questions asked in all reputed MNC like TCS, Infosys, Capgemini, Tech Mahindra, Cognizant, City Bank, Barclays, Amdocs, Mindtree etc. My purpose behind this book is, help you to get a job in your dream company. I can understand this is a struggling period for job seekers and freshers. Everybody must have gone from this phase. so never give up. Keep upgrading yourself. I am sure you will get a job in your dream company. All the best!!! Please reach out to me for personal training for interview preparation.
You can reach out to me at mncjavainterview@gmail.com.
|
2 Comments
Very Informative and creative contents. This concept is a good way to enhance the knowledge. thanks for sharing.
ReplyDeleteContinue to share your knowledge through articles like these, and keep posting more blogs.
And more Information JavaScript Development Services
Thank you Sam.Sure I will keep updating.
Delete