Interviewer And Interviewee Guide

Behavioral Java Multi-Threading Interview Questions & Answers:

1. What is thread synchronization?

In multithreading, the shared resources are allocated to one thread at a time.
multiple thread ensuring that the shared resources will be allocated to only one thread at a time is called thread synchronization.
The synchronized keyword is used to define the critical block in which every Java object get a lock to enter.

Syntax:

Synchronized(object) // object is reference of the object to be synchronized
{
// statement to be synchronized.
}
The synchronized keyword provides locking which maintain the mutual exclusive access of shared resource and race of objects for data.

2. Explain the difference between preemptive scheduling and time slicing?

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.

3. Explain the difference between yielding and sleeping?

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.
Thread.sleep() will moves the thread to “Wait” state.
Thread.yield() will moves the thread to “Ready” state.

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

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.

5. Explain some ways in which a thread can enter the waiting state?

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.

6. Explain Thread?

We can describe "thread" in two ways in Java.

1. An instance of class java.lang.Thread.
2. A thread of execution.

An instance of Thread is an object like all other objects in Java, it also has variables and methods and it lives and dies on the heap.

But a thread of execution is an individual light weight process that has its own call stack.
Even if you don't create any new threads in your program, threads are there running in background.
The main() method runs the main thread. So in main call stack the main() method would be the first.
When a new thread is created then it will have its own stack separate from the main() call stack.

7. What is starvation?

Starvation is a situation when some threads acquired the shared resources for long time and therefore other threads are not able to access those resources and not able to do anything further.
For example, suppose an object provides a synchronized method that often takes a long time to return.
If one thread invokes this method frequently, other threads that also require frequent synchronized access to that object will be blocked.
In Java, Starvation can be caused by inappropriate allocation of thread priorities.
A thread with low priority can be starved by the threads of higher priority if the higher priority threads do not release shared resources time to time.

8. Explain deadlock?

When two or more threads are waiting for each other and get blocked forever then this situation is called Deadlock.
During deadlock two threads, each thread has acquired a lock on one resource and trying to acquire a lock on resource of other thread.
Each thread will wait indefinitely for the other to release the lock, unless one of the user processes is stopped.

In terms of Java API, thread deadlock situation can arise in following conditions:

1. When two threads call Thread.join() on each other.
2. When two threads use nested synchronized blocks to lock two objects and the blocks lock the same objects in different order.

9. How to debug our application for issues when multiple threads are being executed?

Following are some way to debug issues in multi-threaded applications in Java.

-By using logging and print statements along with thread names. In this way we can know about the flow of thread execution.
-With the use of debugging functionality available in Eclipse and JDeveloper.
-We can write a thread dump of the application which will give the information about the active threads at a point of time.

This is most effective way for detecting deadlocks in production systems.

10. What is thread Local variable?

If a variable is declared as threadLocal then all the thread which accesses that variable will maintain its own copy of variable and would not use the copy of any other thread.

For example let consider the scenario of giving JDBC connection to each thread.

public class ThreadLocal {
public Object get();
public void set(Object myValue);
public Object FirstValue();
}

Implementation of ThreadLocal
public class ConnectionDispenser {
private static class ThreadLocalConnection extends ThreadLocal {
public Object FirstValue () {
return DriverManager.getConnection(ConfigurationSingleton.getDbUrl());
}
}

private static ThreadLocalConnection con = new ThreadLocalConnection();
public static Connection MyConnection() {
return (Connection) con.get();
}
}

Copyright 2007-2024 by Interview Questions Answers .ORG All Rights Reserved.
https://InterviewQuestionsAnswers.ORG.