+91 90691 39140 | +1 253 214 3115 | info@hub4tech.com | hub4tech

Top 20 Most Asked Java Multithreading Interview Questions and Answers

Fri, 2017-11-10 17:16
Java Multithreading

Are you preparing for Java Multithreading job interview and wondering what questions you might be asked? In this blog we have hand-picked the questions you are likely to be asked in Java Multithreading job interview and provided to-the-point answers to each one of them so as to help you prepare better for Java Multithreading job interviews.

Always keep in the mind that, only academic knowledge is not enough to crack an interview. Employers expects from the candidate to have practical knowledge and hands-on experience as well. This Java Multithreading Interview Questions designed by industry expert will help you to gain practical knowledge of Java Multithreading.

Java Multithreading Interview Questions and Answers

  1. Preemptive scheduling vs. time slicing.

Answer:
In preemptive scheduling, the highest priority task executes until it enters the waiting or dead states or a higher priority task comes into existence.
In time slicing, a task executes for a predefined slice of time and then reenters the pool of ready tasks.
The scheduler then determines which task should execute next, based on priority and other factors.

  1. What is the difference between Process and Thread?

Answer:
A process is a self contained execution environment and it can be seen as a program or application whereas Thread is a single task of execution within the process. Java runtime environment runs as a single process which contains different classes and programs as processes. Thread can be called lightweight process. Thread requires less resources to create and exists in the process, thread shares the process resources.

  1. What is volatile?

Answer:

  • When more than one threads access the same variable then each thread will maintain its own copy of that variable in the form of local cache.
  • When a thread is changing the value of variable, it is actually changing the local cache not the main variable memory.
  • Every thread which is using the same variable doesn’t know anything about the values changed by another thread.
  • To overcome this problem, if you declare a variable as volatile, then whenever a thread is changing the value of a volatile, it is actually changing to the main memory. So, all other threads which are accessing the same variable can get the changed value.
  1. Which is more preferred – Synchronized method or Synchronized block?

Answer:
Synchronized block is more preferred way because it doesn’t lock the Object, synchronized methods lock the Object and if there are multiple synchronization blocks in the class, even though they are not related, it will stop them from execution and put them in wait state to get the lock on Object.

  1. Briefly explain daemon thread.

Answer:
Daemon thread is a low priority thread which runs in the background performs garbage collection operation for the java runtime system.

  1. What are the benefits of multi-threaded programming?

Answer:
In Multi-Threaded programming, multiple threads are executing concurrently that improves the performance because CPU is not idle incase some thread is waiting to get some resources. Multiple threads share the heap memory, so it’s good to create multiple threads to execute some task rather than creating multiple processes. For example, Servlets are better in performance than CGI because Servlet support multi-threading but CGI doesn’t.

  1. What is the difference between preemptive scheduling and time slicing?

Answer:

  • In Preemptive scheduling, highest priority task will executes until it enters in waiting or dead states. It also executes, until a higher priority task enters.
  • In Time slicing, a task will execute for a fixed time slice and after that it will go in ready state.
  • At that time the scheduler will find the executable task, according to the priority and various other tasks.
  • In preemptive scheduling, the running task will be preempted by the higher priority task.
  • In time slicing methods, a task executes until the specified period of time. Once the execution of that task is complete then the higher priority task will be executed if available.
  1. What is volatile keyword in Java?

Answer:
When we use volatile keyword with a variable, all the threads read it’s value directly from the memory and don’t cache it. This makes sure that the value read is the same as in the memory.

  1. What are the two types of multitasking?

Answer:
Process-based and Thread-based

  1. What is difference between user Thread and daemon Thread?

Answer:
When we create a Thread in java program, it’s known as user thread. A daemon thread runs in background and doesn’t prevent JVM from terminating. When there are no user threads running, JVM shutdown the program and quits. A child thread created from daemon thread is also a daemon thread.

  1. What is the difference between yielding and sleeping?

Answer:

  • Sleep causes the currently executing thread to sleep until the specified time is completed. The thread will resume once the specified time period is over.
  • Sleep causes the currently executing thread to sleep and gives a chance to other threads to execute. The thread will join the ready queue.
  • sleep() will moves the thread to “Wait” state.
  • yield() will moves the thread to “Ready” state.
  1. Why Thread sleep() and yield() methods are static?

Answer:
Thread sleep() and yield() methods work on the currently executing thread. So there is no point in invoking these methods on some other threads that are in wait state. That’s why these methods are made static so that when this method is called statically, it works on the current executing thread and avoid confusion to the programmers who might think that they can invoke these methods on some non-running threads.

  1. Process and Thread

Answer:
A process can contain multiple threads.
A process has its own memory address space whereas a thread doesn't.
Threads share the heap belonging to their parent process.
One process cannot corrupt another process
A thread can write the memory used by another thread.

  1. How can we create a Thread in Java?

Answer:
There are two ways to create Thread in Java – first by implementing Runnable interface and then creating a Thread object from it and second is to extend the Thread Class.

  1. What the difference is between notify and notify All methods?

Answer:

  • The notify will run only first thread which has called wait() on same object. i.e. the object which has called the wait() must be the same as the object that calls
  • The notifyAll will run all threads which has called wait() on same object.
  • While using notifyAll the highest priority thread will run first.
  1. Why wait(), notify() and notifyAll() methods have to be called from synchronized method or block?

Answer:
When a Thread calls wait() on any Object, it must have the monitor on the Object that it will leave and goes in wait state until any other thread call notify() on this Object. Similarly when a thread calls notify() on any Object, it leaves the monitor on the Object and other waiting threads can get the monitor on the Object. Since all these methods require Thread to have the Object monitor, that can be achieved only by synchronization, they need to be called from synchronized method or block.

  1. Name the ways to create the thread.

Answer:
Implementing Runnable and Extending Thread

  1. Can we call run() method of a Thread class?

Answer:
Yes, we can call run() method of a Thread class but then it will behave like a normal method. To actually execute it in a Thread, we need to start it using Thread.start() method.

  1. Can you tell some ways in which a thread can enter the waiting state?

Answer:
A thread can enter the waiting state by the following ways:

  • We can invoke sleep() method of the thread.
  • An attempt to acquire the object’s lock can put the thread in waiting mode.
  • We can also invoke wait() method of the thread.
  • A thread can also be entered in waiting state by invoking its suspend() method.
  1. Why thread communication methods wait(), notify() and notifyAll() are in Object class?

Answer:
In Java every Object has a monitor and wait, notify methods are used to wait for the Object monitor or to notify other threads that Object monitor is free now. There is no monitor on threads in java and synchronization can be used with any Object, that’s why it’s part of Object class so that every class in java has these essential methods for inter thread communication.

Training with Hub4Tech:

To get training on any of the above certifications, you can choose Hub4Tech, which is one of the best online training platforms to get trained in the latest technology. Our industry experts train you through real-time problems and industry-based training. To get the complete information about security architect, please call us at +91 90691 39140

You can also write your queries at info@hub4tech.com. To get training in your favourite course from industry experts, please check our courses details here. Or if preparing yourself, you can also check your assessment level by participating in our online quiz, designed by technology experts based on industry standard.

If you liked the article and want to read other articles as well, read allHub4Tech Blogs here. Stay tuned to our FacebookTwitterand LinkedIn pages for latest updates, course info by Hub4Tech.

Use Full Link for Java Training and Certification

Hitesh Patel
 
Copyright ©2015 Hub4Tech.com, All Rights Reserved. Hub4Tech™ is registered trademark of Hub4tech Portal Services Pvt. Ltd.
All trademarks and logos appearing on this website are the property of their respective owners.
FOLLOW US