{"id":8588,"date":"2019-01-07T12:30:21","date_gmt":"2019-01-07T12:30:21","guid":{"rendered":"https:\/\/www.monsterindia.com\/career-advice\/12-java-multithreading-interview-questions-8588\/"},"modified":"2021-12-24T05:42:31","modified_gmt":"2021-12-24T05:42:31","slug":"12-java-multithreading-interview-questions","status":"publish","type":"post","link":"https:\/\/www.foundit.sg\/career-advice\/12-java-multithreading-interview-questions\/","title":{"rendered":"12 Java Multithreading Interview Questions"},"content":{"rendered":"<p><span style=\"font-size: 10pt;font-family: verdana, geneva, sans-serif\">Most of the lucrative positions in Java Programming require excellent core Java multithreading knowledge and skills. It is a much sought-after skill as only a handful of Java programmers are really hands-on with the multithreading concepts and concurrency problems. <\/span><\/p>\n<p><span style=\"font-size: 10pt;font-family: verdana, geneva, sans-serif\">Although every interview and its scope is different, below is the list of top most frequently asked Java multithreading questions: <\/span><\/p>\n<p><span style=\"font-size: 10pt;font-family: verdana, geneva, sans-serif\">1. What is Multithreading? <\/span><\/p>\n<p><span style=\"font-size: 10pt;font-family: verdana, geneva, sans-serif\">In simple terms, the process of executing many threads simultaneously is called Multithreading. Multithreading is akin to multitasking and is used when no process in the thread can wait for the other process to complete and all have to finish at the same time. <\/span><br \/>\n<span style=\"font-size: 10pt;font-family: verdana, geneva, sans-serif\">2. What exactly is a Thread? <\/span><\/p>\n<p><span style=\"font-size: 10pt;font-family: verdana, geneva, sans-serif\">A thread is a sub-process that has a separate path of execution. Each thread runs independently in its own execution stack. There can be multiple threads in a single process. Even though the processes execute separately, they share process resources. <\/span><\/p>\n<p><span style=\"font-size: 10pt;font-family: verdana, geneva, sans-serif\">3. What are the benefits of Multithreading? <\/span><\/p>\n<p><span style=\"font-size: 10pt;font-family: verdana, geneva, sans-serif\">First and foremost, Multithreading allows for quick execution of tasks as each thread is executed independently. <\/span><\/p>\n<p><span style=\"font-size: 10pt;font-family: verdana, geneva, sans-serif\">In Multithreading, programs are always ready for input in spite of a background task running simultaneously. <\/span><\/p>\n<p><span style=\"font-size: 10pt;font-family: verdana, geneva, sans-serif\">Since threads share the common resources, the cache memory is more efficiently used. <\/span><\/p>\n<p><span style=\"font-size: 10pt;font-family: verdana, geneva, sans-serif\">Server dependency is reduced as one server is capable of executing multiple threads at the same time. <\/span><\/p>\n<p><span style=\"font-size: 10pt;font-family: verdana, geneva, sans-serif\">CPU is never idle waiting for a program thread to free resources. Multiple thread execution is much better than multiple process execution. <\/span><br \/>\n<span style=\"font-size: 10pt;font-family: verdana, geneva, sans-serif\">4. What are the key differences between a process and a Thread? <\/span><br \/>\n<span style=\"font-size: 10pt;font-family: verdana, geneva, sans-serif\">A process is a program in execution. <\/span><\/p>\n<p><span style=\"font-size: 10pt;font-family: verdana, geneva, sans-serif\">A thread is a sub-process. <\/span><\/p>\n<p><span style=\"font-size: 10pt;font-family: verdana, geneva, sans-serif\">Processes have separate memory addresses whereas threads share address space in memory. <\/span><\/p>\n<p><span style=\"font-size: 10pt;font-family: verdana, geneva, sans-serif\">Context switching between processes is faster than context switching between threads. <\/span><\/p>\n<p><span style=\"font-size: 10pt;font-family: verdana, geneva, sans-serif\">Communication between threads is much faster than communication between processes. <\/span><\/p>\n<p><span style=\"font-size: 10pt;font-family: verdana, geneva, sans-serif\">Since processes don\u2019t share any resources, any change to the parent process won\u2019t affect the child process whereas changes to the parent thread will impact the child thread. <\/span><\/p>\n<p><span style=\"font-size: 10pt;font-family: verdana, geneva, sans-serif\">5. Explain Inter thread communication. <\/span><\/p>\n<p><span style=\"font-size: 10pt;font-family: verdana, geneva, sans-serif\">Communication between 2 or more synchronized threads is termed as inter thread communication. Inter thread communication helps to prevent thread polling in Java. <\/span><\/p>\n<p><span style=\"font-size: 10pt;font-family: verdana, geneva, sans-serif\">Even if one thread is executing and then paused, another thread can enter or lock into the same point and still be executed. <\/span><\/p>\n<p><span style=\"font-size: 10pt;font-family: verdana, geneva, sans-serif\">wait(), notifyall(), notify() methods are used to implement inter thread communication. <\/span><\/p>\n<p><span style=\"font-size: 10pt;font-family: verdana, geneva, sans-serif\">6. Explain the function of wait() method in Java. <\/span><\/p>\n<p><span style=\"font-size: 10pt;font-family: verdana, geneva, sans-serif\">The wait() method is provided by the Object class in Java for the purpose of inter thread communication. Java.lang.Object.wait() is the method to pause the current thread. The thread is paused until another thread calls the notify() or notifyall() method. <\/span><\/p>\n<p><span style=\"font-size: 10pt;font-family: verdana, geneva, sans-serif\">The wait() method must be called from the synchronised method for proper communication along with notify() and notifyall() methods. <\/span><\/p>\n<p><span style=\"font-size: 10pt;font-family: verdana, geneva, sans-serif\">7. Explain the life cycle of a Thread. <\/span><\/p>\n<p><span style=\"font-size: 10pt;font-family: verdana, geneva, sans-serif\">A thread can go through any of the following stages in its life cycle: <\/span><\/p>\n<p><span style=\"font-size: 10pt;font-family: verdana, geneva, sans-serif\">New: In the New state, a Thread class is initiated using the New operator but boot yet started. <\/span><\/p>\n<p><span style=\"font-size: 10pt;font-family: verdana, geneva, sans-serif\">Runnable: In this state, the thread is started using the start() method. <\/span><\/p>\n<p><span style=\"font-size: 10pt;font-family: verdana, geneva, sans-serif\">Running: This is the state where the thread scheduler picks up the thread from the ready state and makes in running. <\/span><\/p>\n<p><span style=\"font-size: 10pt;font-family: verdana, geneva, sans-serif\">Waiting: This state, also called the Blocked state, is alive but running as it is waiting for another thread to finish or a resource to free up. <\/span><\/p>\n<p><span style=\"font-size: 10pt;font-family: verdana, geneva, sans-serif\">Terminated: A thread is in the dead or terminated state when it exits with the run() method. <\/span><\/p>\n<p><span style=\"font-size: 10pt;font-family: verdana, geneva, sans-serif\">8. Explain Context Switching. <\/span><\/p>\n<p><span style=\"font-size: 10pt;font-family: verdana, geneva, sans-serif\">The state of the thread or the process is stored in Context Switching so that it can be restored and execution resumed at a later point of time. In this way, context switching facilitates multiple processes to share CPU resource. <\/span><\/p>\n<p><span style=\"font-size: 10pt;font-family: verdana, geneva, sans-serif\">9. Explain different ways to create a Thread. <\/span><br \/>\n<span style=\"font-size: 10pt;font-family: verdana, geneva, sans-serif\">There are two ways to create a thread- by extending the thread class or by implementing the thread class via Interface. The differences in these both methods are: <\/span><\/p>\n<p><span style=\"font-size: 10pt;font-family: verdana, geneva, sans-serif\">If we extend from the Thread class, we can\u2019t extend from any other class as Java doesn\u2019t permit multiple inheritance. But if the Runnable interface is implemented, even the base class can be extended, if needed. <\/span><\/p>\n<p><span style=\"font-size: 10pt;font-family: verdana, geneva, sans-serif\">If we extend from the Thread class, each associated thread can create a unique object. While implementing a Runnable interface, multiple threads have to share the same object. <\/span><\/p>\n<p><span style=\"font-size: 10pt;font-family: verdana, geneva, sans-serif\">There are various inbuilt methods in Thread class such as getPriority(), isAlive and so on while runnable interface provides only one method, run(). <\/span><\/p>\n<p><span style=\"font-size: 10pt;font-family: verdana, geneva, sans-serif\">10. Differentiate between the wait() and sleep() method. <\/span><br \/>\n<span style=\"font-size: 10pt;font-family: verdana, geneva, sans-serif\">The wait() method has to be defined in the Object class while the sleep() method is defined in the Thread class. <\/span><\/p>\n<p><span style=\"font-size: 10pt;font-family: verdana, geneva, sans-serif\">Implementing the wait() method releases the lock whereas implementing the sleep() method does not release the lock. <\/span><\/p>\n<p><span style=\"font-size: 10pt;font-family: verdana, geneva, sans-serif\">11. Can a Thread be started twice? <\/span><\/p>\n<p><span style=\"font-size: 10pt;font-family: verdana, geneva, sans-serif\">A thread cannot be started twice as once a thread is started and executed, it goes to the Dead state. If an attempt is made to restart the thread, the compiler will throw a runtime exception. <\/span><\/p>\n<p><span style=\"font-size: 10pt;font-family: verdana, geneva, sans-serif\">12. What does Synchronization mean? <\/span><\/p>\n<p><span style=\"font-size: 10pt;font-family: verdana, geneva, sans-serif\">When multiple threads are trying to access the same resources, synchronization is needed to control the access to avoid any consistency issues as well as thread interference. <\/span><\/p>\n<p><span style=\"font-size: 10pt;font-family: verdana, geneva, sans-serif\">Java implements the process of synchronization to allow only a single thread to execute at a time. This prevents errors from multiple thread execution at once.\u202f <\/span><\/p>\n<p><span style=\"font-size: 10pt;font-family: verdana, geneva, sans-serif\">Synchronization can be implemented either by the synchronized method, synchronized block, or by static synchronization.<\/span><\/p>\n<p><span style=\"font-size: 10pt;font-family: verdana, geneva, sans-serif\">Looking for Multithreading Jobs in top cities? Click at the links below:<\/span><br \/>\n<span style=\"font-size: 10pt;font-family: verdana, geneva, sans-serif\">\u2022 Multithreading Jobs in Delhi<\/span><br \/>\n<span style=\"font-size: 10pt;font-family: verdana, geneva, sans-serif\">\u2022 Multithreading Jobs in Mumbai<\/span><br \/>\n<span style=\"font-size: 10pt;font-family: verdana, geneva, sans-serif\">\u2022 Multithreading Jobs in Chennai<\/span><br \/>\n<span style=\"font-size: 10pt;font-family: verdana, geneva, sans-serif\">\u2022 Multithreading Jobs in Gurgaon<\/span><br \/>\n<span style=\"font-size: 10pt;font-family: verdana, geneva, sans-serif\">\u2022 Multithreading Jobs in Pune<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Most of the lucrative positions in Java Programming require excellent core Java multithreading knowledge and skills. It is a much sought-after skill as only a handful of Java programmers are really hands-on with the multithreading concepts and concurrency problems. Although every interview and its scope is different, below is the list of top most frequently [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":11436,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[147],"tags":[],"class_list":{"0":"post-8588","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-interview-questions"},"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.foundit.sg\/career-advice\/wp-json\/wp\/v2\/posts\/8588","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.foundit.sg\/career-advice\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.foundit.sg\/career-advice\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.foundit.sg\/career-advice\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.foundit.sg\/career-advice\/wp-json\/wp\/v2\/comments?post=8588"}],"version-history":[{"count":1,"href":"https:\/\/www.foundit.sg\/career-advice\/wp-json\/wp\/v2\/posts\/8588\/revisions"}],"predecessor-version":[{"id":11484,"href":"https:\/\/www.foundit.sg\/career-advice\/wp-json\/wp\/v2\/posts\/8588\/revisions\/11484"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.foundit.sg\/career-advice\/wp-json\/wp\/v2\/media\/11436"}],"wp:attachment":[{"href":"https:\/\/www.foundit.sg\/career-advice\/wp-json\/wp\/v2\/media?parent=8588"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.foundit.sg\/career-advice\/wp-json\/wp\/v2\/categories?post=8588"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.foundit.sg\/career-advice\/wp-json\/wp\/v2\/tags?post=8588"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}