Java concurrency as well as thread interview questions answers
Multithreading is an of import characteristic of Java programming language, which agency threads are too an of import purpose of whatsoever Java interview. It's truthful as well as inward fact at beginners as well as freshers bird Thread interview questions inward Java are 1 of most hard to answer. One argue for interview query related to multithreading as well as concurrency beingness hard is confusion unopen to how multiple threads plant together as well as minute is threads are really a complicated theme to empathise as well as usage correctly. Mostly thread interview questions checks Java programmers noesis on Java Thread API, Java concurrency API, issues related to multi-threading similar a race condition, thread-safety, as well as deadlock. Some fourth dimension multithreading as well as concurrency interview query too focus on parallel blueprint patterns similar solving the producer-consumer problem, implementing travel bag pattern or solving dining philosopher work inward Java. This is especially truthful spell interviewing experienced Java developers amongst iv to half-dozen years of experience.
In this article, nosotros volition withdraw maintain a expression at dissimilar kinds of multithreading as well as concurrency questions asked inward diverse interviews e.g. on telephonic or human face upward to human face upward interview, on written test, to both experienced as well as senior Java developers, as well as some tips to reply them correctly.
Questions asked on telephonic or the first circular of interviews are tend to travel easier as well as you lot must reply them to the betoken amongst a keyword, which interviewer is expecting. On the human face upward to human face upward interview, travel laid upward for dissimilar kinds of follow-up questions.
By the way, these thread interview questions are as useful for senior Java developers or guys amongst some Java sense inward hand. I withdraw maintain tried to percentage answers to these interview questions on the thread as good but I propose you lot do some enquiry as well as larn the theme good to reply whatsoever follow-up questions, which comes due to your response to these thread questions inward Java.
Anyway hither is my collection of Java thread interview questions as well as how to answers them inward Java :
1) What is the departure betwixt start as well as run method inward Java Thread? (answer)
This thread interview query is too inquire as if start() method eventually telephone phone run() method as well as then why do you lot demand to telephone phone start() method, why non telephone phone run() method directly. well, argue is that because start method creates a novel thread as well as telephone phone the code written within the run method on a novel thread spell calling run method executes that code on the same thread. You tin too come across the article start vs run method inward Java for to a greater extent than details.
2) Write code to avoid deadlock inward Java where due north threads are accessing due north shared resources? (answer)
This is a classic Java multithreading interview questions, which appears on almost every listing of Java thread questions. This query is based on jeopardy as well as issues faced past times parallel programs without proper synchronization or wrong synchronization. This query explores the concept of looking as well as best practices on acquiring as well as releasing the lock on shared resource. By the way, it's been covered inward many places as good as well as I propose reading How to preclude deadlock inward Java, non exclusively for particular reply of this Java multithreading query but too to larn how to preclude deadlock inward Java.
3) Which 1 is improve to implement thread inward Java ? extending Thread degree or implementing Runnable? (answer)
Well, this is some other ofttimes asked questions on whatsoever Java thread interview. Essentially these are ii way to implement Thread inward Java, past times extending java.lang.Thread degree or past times implementing java.lang.Runnable interface.
By extending the degree you lot are using your adventure to extend 1 whatsoever exclusively 1 degree as Java does non back upward multiple inheritances, past times implementing a Runnable interface you lot tin yet extend some other class. So extending Runnable or fifty-fifty Callable is a improve choice. You tin too come across Runnable vs Thread degree inward Java for to a greater extent than answers on this questions.
Given its simplicity as well as fact-based nature, this query generally appears on either telephonic circular or initial screening rounds. Key points to mention, spell answering this query includes multiple inheritance at the degree bird as well as separation of defining a chore as well as execution of a task. Runnable exclusively stand upward for a task, spell Thread stand upward for both tasks as well as it's execution.
4) What is Busy Spinning? Why volition you lot usage Busy Spinning as hold off strategy? (answer)
This is 1 of the advanced concurrency interview questions inward Java as well as exclusively asked to experienced as well as senior Java developers, amongst lots of concurrent coding sense nether the belt. By the way, the concept of busy spinning is non new, but its usage amongst multi-core processor has risen lately (see The Art of Multiprocessor Programming).
The busy waiting is a hold off strategy, where 1 thread hold off for a status to overstep away true, but instead of calling hold off or slumber method as well as releasing CPU, it only spins. This is specially useful if the status is going to travel truthful quite chop-chop i.e. inward a millisecond or microsecond.
The wages of non releasing CPU is that all cached information as well as teaching stay unaffected, which may travel lost, had this thread is suspended on 1 heart as well as brought dorsum to some other thread. If you lot tin reply this question, that residual assure of a practiced impression.
5) What is the departure betwixt CountDownLatch and CyclicBarrier in Java? (answer)
The CountDownLatch and CyclicBarrier inward Java are ii of import concurrency utility which is added on Java v Concurrency API. Both are used to implement scenario, where 1 thread has to hold off for other thread earlier starting processing but at that spot is a departure betwixt them.
The fundamental betoken to mention, spell answering this query is that CountDownLatch is non reusable in 1 trial the count reaches to zero, spell CyclicBarrier tin travel reused fifty-fifty after the barrier is broken.
You tin too come across my previous article difference betwixt CyclicBarrier as well as CountDownLatch inward Java for a to a greater extent than detailed reply of this concurrency interview query as well as a existent life trial of where to usage these concurrency utilities.
6) What is the departure betwixt hold off as well as slumber inward Java? (method)
One to a greater extent than classic Java multithreading question from the telephonic circular of interviews. The fundamental betoken to advert spell answering this query is to advert that hold off volition release the lock as well as must travel called from the synchronized context, spell slumber volition exclusively intermission the thread for some fourth dimension as well as overstep away on the lock.
By the way, both methods throw IntrupptedException and tin travel interrupted, which tin Pb to some follow-up questions like, tin nosotros awake a sleeping or waiting for a thread inward Java? You tin too read a detailed reply on my postal service of same championship here.
7) How do you lot solve producer consumer work inward Java? (solution)
One of my favorite questions during whatsoever Java multithreading interview, Almost one-half of the concurrency problems tin travel categorized inward the producer-consumer pattern. There are basically ii ways to solve this work inward Java, One past times using hold off as well as notify method as well as other past times using BlockingQueue in Java. later is slow to implement as well as a practiced selection if you lot are coding inward Java 5.
The fundamental points to mention, spell answering this query is thread-safety and blocking nature of BlockingQueue as well as how that helps, spell writing concurrent code. You tin too expression lots of follow-up questions including, what give off if you lot withdraw maintain multiple producer or multiple consumers, what volition give off if a producer is faster than consumer thread or vice-versa. You tin too come across this link for trial of how to code producer-consumer blueprint inward Java using blocking queue
8) Why is ConcurrentHashMap faster than Hashtable inward Java? (answer)
ConcurrentHashMap is introduced as an alternative of Hashtable inward Java 5, it is faster because of its design. ConcurrentHashMap divides the whole map into dissimilar segments as well as exclusively lock a particular segment during the update operation, instead of Hashtable, which locks whole Map.
The ConcurrentHashMap also provides lock-free read, which is non possible inward Hashtable, because of this as well as lock striping, ConcurrentHashMap is faster than Hashtable, especially when a number of the reader is to a greater extent than than the number of writers.
In lodge to improve reply this pop Java concurrency interview questions, I propose reading my postal service almost the internal working of ConcurrentHashMap inward Java.
9) What is the departure betwixt submit() as well as execute() method of Executor as well as ExecutorService inward Java? (answer)
The primary departure betwixt submit as well as execute method from ExecutorService interface is that one-time render a termination inward the shape of a Future object, spell afterward doesn't render a result. By the way, both are used to submit a chore to thread puddle inward Java but 1 is defined inward Executor interface,while other is added into ExecutorService interface. This multithreading interview query is too asked inward the commencement circular of Java interviews.
10) How do you lot percentage information betwixt ii threads inward Java? (answer)
One to a greater extent than Java multithreading query from the telephonic circular of interview. You tin percentage information betwixt thread past times using shared object or shared information structures similar Queue. Depending upon, what you lot are using, you lot demand to render the thread-safety guarantee, as well as 1 way of providing thread-safety is using synchronized keyword.
If you lot usage concurrent collection classes from Java v e.g. BlockingQueue, you lot tin easily percentage information without beingness bothered almost thread security as well as inter-thread communication. I similar this thread question, because of it's simplicity as well as effectiveness. This too leads farther follow-up questions on issues which arise due to sharing information betwixt threads e.g. race conditions.
11) What is ReentrantLock inward Java? Have you lot used it before? (answer)
ReentrantLock is an alternative of synchronized keyword inward Java, it is introduced to handgrip some of the limitations of synchronized keywords. Many concurrency utility classes as well as concurrent collection classes from Java 5, including ConcurrentHashMap uses ReentrantLock, to leverage optimization.
The ReentrantLock generally uses an atomic variable as well as faster CAS functioning to provides improve performance. Key points to advert are the difference betwixt ReentrantLock as well as synchronized keyword inward Java, which includes the mightiness to acquire lock interruptibly, timeout characteristic spell waiting for lock etc. ReentrantLock too gives the alternative to do fair lock inward Java.Once in 1 trial to a greater extent than a rattling practiced Java concurrency interview query for experienced Java programmers.
Books similar Java Programming Interview exposed too helps you lot to laid upward improve as well as reply to the point.
12) What is ReadWriteLock inward Java? What is the do goodness of using ReadWriteLock inward Java?(answer)
This is ordinarily a follow-up query of previous Java concurrency questions. The ReadWriteLock is in 1 trial to a greater extent than based upon the concept of lock striping, 1 of the advance thread-safety machinery which advocates separating locks for reading as well as writing operations (see Java Concurrency inward Practice Bundle past times Heinz Kabutz for to a greater extent than details).
If you lot withdraw maintain noticed before, reading functioning tin travel done without locking if at that spot is no author as well as that tin hugely improve the performance of whatsoever application. The ReadWriteLock leverage this thought as well as render policies to allow maximum concurrency level. Java Concurrency API too provides an implementation of this concept as ReentrantReadWriteLock.
Depending upon Interviewer as well as sense of the candidate, you lot tin fifty-fifty expression to render your ain implementation of ReadWriteLock, thence travel laid upward for that as well.
Here is a prissy diagram which clearly explains working of read-write lock inward Java:
These were some of my favorite interview questions based on multithreading as well as concurrent inward Java. Threading as well as Concurrency is a big theme inward Java as well as has lots of interesting, tricky as well as tough question but for starters as well as freshers, these questions sure aid to clear whatsoever thread interview inward Java.
As I said, mentioning the fundamental points are rattling of import spell answering questions on multithreading as well as concurrency. I too propose farther reading Java Concurrency inward Practice to larn to a greater extent than almost locking, synchronization, concurrent collections as well as concurrency utility classes to do good inward heart Java as well as multithreading interviews.
Further Learning
Multithreading as well as Parallel Computing inward Java
15 Technical Core Java Questions asked to Experienced Programmer on Interviews
Good Questions on Java Collection as well as Concurrent Collection amongst answers
Top 10 Java Design Pattern Questions as well as How to reply them
10 Spring MVC Interview Questions for Java Programmers
10 Java Enum Interview Questions Answers for beginners
Top 10 JDBC Interview Questions for Experienced Programmers
Top 10 RESTful Web Service Interview Questions as well as Answers
Multithreading is an of import characteristic of Java programming language, which agency threads are too an of import purpose of whatsoever Java interview. It's truthful as well as inward fact at beginners as well as freshers bird Thread interview questions inward Java are 1 of most hard to answer. One argue for interview query related to multithreading as well as concurrency beingness hard is confusion unopen to how multiple threads plant together as well as minute is threads are really a complicated theme to empathise as well as usage correctly. Mostly thread interview questions checks Java programmers noesis on Java Thread API, Java concurrency API, issues related to multi-threading similar a race condition, thread-safety, as well as deadlock. Some fourth dimension multithreading as well as concurrency interview query too focus on parallel blueprint patterns similar solving the producer-consumer problem, implementing travel bag pattern or solving dining philosopher work inward Java. This is especially truthful spell interviewing experienced Java developers amongst iv to half-dozen years of experience.
In this article, nosotros volition withdraw maintain a expression at dissimilar kinds of multithreading as well as concurrency questions asked inward diverse interviews e.g. on telephonic or human face upward to human face upward interview, on written test, to both experienced as well as senior Java developers, as well as some tips to reply them correctly.
Questions asked on telephonic or the first circular of interviews are tend to travel easier as well as you lot must reply them to the betoken amongst a keyword, which interviewer is expecting. On the human face upward to human face upward interview, travel laid upward for dissimilar kinds of follow-up questions.
Java Interview questions on Concurrency as well as multithreading
As I said, inward this Java article, non only, I volition percentage some of the most commonly asked thread interview questions at freshers as well as beginners level, similar upward to 2 to iv years of experience, as well as some tips as well as tricks to reply them correctly.By the way, these thread interview questions are as useful for senior Java developers or guys amongst some Java sense inward hand. I withdraw maintain tried to percentage answers to these interview questions on the thread as good but I propose you lot do some enquiry as well as larn the theme good to reply whatsoever follow-up questions, which comes due to your response to these thread questions inward Java.
Anyway hither is my collection of Java thread interview questions as well as how to answers them inward Java :
1) What is the departure betwixt start as well as run method inward Java Thread? (answer)
This thread interview query is too inquire as if start() method eventually telephone phone run() method as well as then why do you lot demand to telephone phone start() method, why non telephone phone run() method directly. well, argue is that because start method creates a novel thread as well as telephone phone the code written within the run method on a novel thread spell calling run method executes that code on the same thread. You tin too come across the article start vs run method inward Java for to a greater extent than details.
2) Write code to avoid deadlock inward Java where due north threads are accessing due north shared resources? (answer)
This is a classic Java multithreading interview questions, which appears on almost every listing of Java thread questions. This query is based on jeopardy as well as issues faced past times parallel programs without proper synchronization or wrong synchronization. This query explores the concept of looking as well as best practices on acquiring as well as releasing the lock on shared resource. By the way, it's been covered inward many places as good as well as I propose reading How to preclude deadlock inward Java, non exclusively for particular reply of this Java multithreading query but too to larn how to preclude deadlock inward Java.
3) Which 1 is improve to implement thread inward Java ? extending Thread degree or implementing Runnable? (answer)
Well, this is some other ofttimes asked questions on whatsoever Java thread interview. Essentially these are ii way to implement Thread inward Java, past times extending java.lang.Thread degree or past times implementing java.lang.Runnable interface.
By extending the degree you lot are using your adventure to extend 1 whatsoever exclusively 1 degree as Java does non back upward multiple inheritances, past times implementing a Runnable interface you lot tin yet extend some other class. So extending Runnable or fifty-fifty Callable is a improve choice. You tin too come across Runnable vs Thread degree inward Java for to a greater extent than answers on this questions.
Given its simplicity as well as fact-based nature, this query generally appears on either telephonic circular or initial screening rounds. Key points to mention, spell answering this query includes multiple inheritance at the degree bird as well as separation of defining a chore as well as execution of a task. Runnable exclusively stand upward for a task, spell Thread stand upward for both tasks as well as it's execution.
4) What is Busy Spinning? Why volition you lot usage Busy Spinning as hold off strategy? (answer)
This is 1 of the advanced concurrency interview questions inward Java as well as exclusively asked to experienced as well as senior Java developers, amongst lots of concurrent coding sense nether the belt. By the way, the concept of busy spinning is non new, but its usage amongst multi-core processor has risen lately (see The Art of Multiprocessor Programming).
The busy waiting is a hold off strategy, where 1 thread hold off for a status to overstep away true, but instead of calling hold off or slumber method as well as releasing CPU, it only spins. This is specially useful if the status is going to travel truthful quite chop-chop i.e. inward a millisecond or microsecond.
The wages of non releasing CPU is that all cached information as well as teaching stay unaffected, which may travel lost, had this thread is suspended on 1 heart as well as brought dorsum to some other thread. If you lot tin reply this question, that residual assure of a practiced impression.
5) What is the departure betwixt CountDownLatch and CyclicBarrier in Java? (answer)
The CountDownLatch and CyclicBarrier inward Java are ii of import concurrency utility which is added on Java v Concurrency API. Both are used to implement scenario, where 1 thread has to hold off for other thread earlier starting processing but at that spot is a departure betwixt them.
The fundamental betoken to mention, spell answering this query is that CountDownLatch is non reusable in 1 trial the count reaches to zero, spell CyclicBarrier tin travel reused fifty-fifty after the barrier is broken.
You tin too come across my previous article difference betwixt CyclicBarrier as well as CountDownLatch inward Java for a to a greater extent than detailed reply of this concurrency interview query as well as a existent life trial of where to usage these concurrency utilities.
6) What is the departure betwixt hold off as well as slumber inward Java? (method)
One to a greater extent than classic Java multithreading question from the telephonic circular of interviews. The fundamental betoken to advert spell answering this query is to advert that hold off volition release the lock as well as must travel called from the synchronized context, spell slumber volition exclusively intermission the thread for some fourth dimension as well as overstep away on the lock.
By the way, both methods throw IntrupptedException and tin travel interrupted, which tin Pb to some follow-up questions like, tin nosotros awake a sleeping or waiting for a thread inward Java? You tin too read a detailed reply on my postal service of same championship here.
7) How do you lot solve producer consumer work inward Java? (solution)
One of my favorite questions during whatsoever Java multithreading interview, Almost one-half of the concurrency problems tin travel categorized inward the producer-consumer pattern. There are basically ii ways to solve this work inward Java, One past times using hold off as well as notify method as well as other past times using BlockingQueue in Java. later is slow to implement as well as a practiced selection if you lot are coding inward Java 5.
The fundamental points to mention, spell answering this query is thread-safety and blocking nature of BlockingQueue as well as how that helps, spell writing concurrent code. You tin too expression lots of follow-up questions including, what give off if you lot withdraw maintain multiple producer or multiple consumers, what volition give off if a producer is faster than consumer thread or vice-versa. You tin too come across this link for trial of how to code producer-consumer blueprint inward Java using blocking queue
8) Why is ConcurrentHashMap faster than Hashtable inward Java? (answer)
ConcurrentHashMap is introduced as an alternative of Hashtable inward Java 5, it is faster because of its design. ConcurrentHashMap divides the whole map into dissimilar segments as well as exclusively lock a particular segment during the update operation, instead of Hashtable, which locks whole Map.
The ConcurrentHashMap also provides lock-free read, which is non possible inward Hashtable, because of this as well as lock striping, ConcurrentHashMap is faster than Hashtable, especially when a number of the reader is to a greater extent than than the number of writers.
In lodge to improve reply this pop Java concurrency interview questions, I propose reading my postal service almost the internal working of ConcurrentHashMap inward Java.
9) What is the departure betwixt submit() as well as execute() method of Executor as well as ExecutorService inward Java? (answer)
The primary departure betwixt submit as well as execute method from ExecutorService interface is that one-time render a termination inward the shape of a Future object, spell afterward doesn't render a result. By the way, both are used to submit a chore to thread puddle inward Java but 1 is defined inward Executor interface,while other is added into ExecutorService interface. This multithreading interview query is too asked inward the commencement circular of Java interviews.
10) How do you lot percentage information betwixt ii threads inward Java? (answer)
One to a greater extent than Java multithreading query from the telephonic circular of interview. You tin percentage information betwixt thread past times using shared object or shared information structures similar Queue. Depending upon, what you lot are using, you lot demand to render the thread-safety guarantee, as well as 1 way of providing thread-safety is using synchronized keyword.
If you lot usage concurrent collection classes from Java v e.g. BlockingQueue, you lot tin easily percentage information without beingness bothered almost thread security as well as inter-thread communication. I similar this thread question, because of it's simplicity as well as effectiveness. This too leads farther follow-up questions on issues which arise due to sharing information betwixt threads e.g. race conditions.
11) What is ReentrantLock inward Java? Have you lot used it before? (answer)
ReentrantLock is an alternative of synchronized keyword inward Java, it is introduced to handgrip some of the limitations of synchronized keywords. Many concurrency utility classes as well as concurrent collection classes from Java 5, including ConcurrentHashMap uses ReentrantLock, to leverage optimization.
The ReentrantLock generally uses an atomic variable as well as faster CAS functioning to provides improve performance. Key points to advert are the difference betwixt ReentrantLock as well as synchronized keyword inward Java, which includes the mightiness to acquire lock interruptibly, timeout characteristic spell waiting for lock etc. ReentrantLock too gives the alternative to do fair lock inward Java.Once in 1 trial to a greater extent than a rattling practiced Java concurrency interview query for experienced Java programmers.
Books similar Java Programming Interview exposed too helps you lot to laid upward improve as well as reply to the point.
12) What is ReadWriteLock inward Java? What is the do goodness of using ReadWriteLock inward Java?(answer)
This is ordinarily a follow-up query of previous Java concurrency questions. The ReadWriteLock is in 1 trial to a greater extent than based upon the concept of lock striping, 1 of the advance thread-safety machinery which advocates separating locks for reading as well as writing operations (see Java Concurrency inward Practice Bundle past times Heinz Kabutz for to a greater extent than details).
If you lot withdraw maintain noticed before, reading functioning tin travel done without locking if at that spot is no author as well as that tin hugely improve the performance of whatsoever application. The ReadWriteLock leverage this thought as well as render policies to allow maximum concurrency level. Java Concurrency API too provides an implementation of this concept as ReentrantReadWriteLock.
Depending upon Interviewer as well as sense of the candidate, you lot tin fifty-fifty expression to render your ain implementation of ReadWriteLock, thence travel laid upward for that as well.
Here is a prissy diagram which clearly explains working of read-write lock inward Java:
These were some of my favorite interview questions based on multithreading as well as concurrent inward Java. Threading as well as Concurrency is a big theme inward Java as well as has lots of interesting, tricky as well as tough question but for starters as well as freshers, these questions sure aid to clear whatsoever thread interview inward Java.
As I said, mentioning the fundamental points are rattling of import spell answering questions on multithreading as well as concurrency. I too propose farther reading Java Concurrency inward Practice to larn to a greater extent than almost locking, synchronization, concurrent collections as well as concurrency utility classes to do good inward heart Java as well as multithreading interviews.
Further Learning
Multithreading as well as Parallel Computing inward Java
15 Technical Core Java Questions asked to Experienced Programmer on Interviews
Good Questions on Java Collection as well as Concurrent Collection amongst answers
Top 10 Java Design Pattern Questions as well as How to reply them
10 Spring MVC Interview Questions for Java Programmers
10 Java Enum Interview Questions Answers for beginners
Top 10 JDBC Interview Questions for Experienced Programmers
Top 10 RESTful Web Service Interview Questions as well as Answers
No comments:
Post a Comment