Online Tutorials & Training Materials | STechies.com
Register Login

Java Multithreading Interview Questions and Answers

|| 0

Java Multithreading Interview Questions and Answers
Stechies

What is multithreading in Java?

In Java, multithreading allows two or more parts of a program to run together. Here, the CPU usage is maximized by the process. The two sections of the program are called threads. A thread is a lightweight process.

Multithreading is used in animation and game development.

What are the advantages of multithreading in Java?

The advantages of multithreading in Java are:

  • Better utilization of CPU resources resulting in faster development time
  • Even if an exception occurs in a thread, the other threads remain unaffected
  • Enhanced GUI responsiveness
  • Resource sharing by threads allows an application to have different threads in an address space

Difference between multitasking and multithreading in Java?

The differences between multitasking and multithreading in Java are:

                      Multithreading 

                        Multitasking 

A process is divided into subprocess and they are executed independently  

More than one task is executed simultaneously

Different components of a single process are executed at a time

Jobs are executed one by one at a single time

Usually, there is a single user

Users can be more than one

The threads share resources and memory allocated to the main process

Here, separate resources and memory has to be allocated to each task being executed

What are the different ways of achieving multithreading in Java?

The different ways of achieving multithreading in Java are:

  • By implementing the Runnable interface
  • By extending Thread class – A class is created that extends the java.lang.Thread class

How to run multithreading in Java?

The easiest method to run multithreading in Java is to create a class and then implement the Runnable interface using that class. Inside this class, we can have a public function that can be accessed by all and has a simple PrintLN statement. Then a Thread object is created and the start() method is called on that object.

What is a deadlock in Java multithreading?

In Java multithreading, consider a situation where a thread is waiting for obtaining an object lock. This lock may be with another thread, which is waiting for the first thread to release its lock. As both threads are waiting for each other to release their respective locks, a Deadlock occurs.

How to avoid a deadlock in multithreading Java?

The best way to prevent deadlock is by a circular wait without pre-emption. Here, the locks are requested by the threads in the same order. Therefore, if a thread acquires a lock, another thread will not raise a request for that lock before the first thread has released it.

What is context switching in multithreading Java?

Context switching is the process of preserving and restoring the current state of a CPU. This is done because a thread execution may be halted due to some interruption, but the process will allow it to resume the execution from that point later. The process is crucial for multitasking and multithreading environment.

What is daemon thread in Java multithreading?

It is a low priority thread that runs in the background and provides services to the user thread. Its life depends upon the user thread, so when these threads complete execution, the daemon thread dies. It cannot prevent the JVM to exit when the user threads have finished execution.

These threads are used for garbage collection. Example, GC thread, finalizer thread.

Explain race condition in Java multithreading?

In a multithreaded environment, a race condition occurs when multiple threads want to access the same shared resource at a particular point of time. Both of the threads race against each other to modify or update the resource. The condition is considered safe if the threads are trying to read the resource and not update it.

What is the difference between calling wait() and sleep() method?

The wait() method is an instance method and is used for synchronization. It can only be called from a synchronized block on any object. It releases the object’s lock so that another thread can acquire it. When the wait() is called, the thread goes to waiting state and can be woken up by the notify() and notifyAll() method.

The sleep() method pauses the thread and the locks are not released. The thread is paused for some time and is restarted after a specific interval.

Explain thread pool in multithreading?

In a thread pool, a group of worker threads having fixed size are created. From the pool, the service provider picks out a thread amongst many. A job is then allocated to the thread. After the job is executed, the thread is included back in the pool again. Using this method, previously created threads can be reused to reduce resource thrashing and thread cycle overhead.

What is synchronization in multithreading in Java?

Synchronization is the process of ensuring that a shared resource is accessed by one thread at a time. The synchronized keyword is used for creating a code block that is called a critical section. The object within this section has a lock on it. The thread that wants to access the object has to acquire the lock, and release it when work is done. Only then the other thread can access it.

Explain Shutdown Hook in multithreading?

Shutdown Hook is a facility that allows developers to execute a piece of code when the JVM shuts down. This became useful when we require cleaning up resources and saving the JVM state while it’s shutting down. This can be done by creating a class that extends the java.lang.Thread class. The code statements can be placed inside the public void run() method.

What happens if we call the run() method instead of start() method?      

If a run() method is called through the start() method, a new thread is allocated to the execution of the method. So if multiple threads call start(), the run() will be executed separately by the threads.

But if run() is called directly, the execution will be handled by a single thread and no multithreading takes place. This will affect the sequential execution of the threads.

How will run() be called if we call start() in multithreading?

When the start() method is called by a program, a new thread is created. The run() method is called after that and all the code inside will be executed. But start() cannot be called twice or else an IllegalStateException will be thrown. But run() can be called as a normal method.

How to read and write a file using multithreading in Java?

The java FileWriter and FileReader classes are used to read and write files.