Skip to main content

Synchronization Interview Questions in Java

In this post I am sharing frequently asking Synchronization Interview Questions for Java Developers either Freshers or Experienced candidates. When you would face Technical Interview you must face these questions. I strongly Recommended before attend the interview must brush up the following questions.

1) What is synchronization?

Ans: Synchronization is the capability of control the access of multiple threads to any shared resource. 'synchronized' is the keyword applicable for the methods and blocks. If a method is declared as synchronized then at a time only one thread is allowed to execute that method on the given object. The main advantage of synchronized keyword is we can overcome data inconsistency problems. 

2) What class level lock?

Ans: If a thread wants to execute any static synchronized method then compulsory that thread should require class level lock. While a thread executing any static system method then remaining threads are not allowed to execute any static synchronized method of the same class simultaneously. But the remaining threads are allowed to execute any non-synchronized static methods.

3) What is difference between Synchronized method and block?

Ans:  When thread executing a synchronized method on the object,then the remaining threads are not allowed to execute any synchronized method on the same object .

Synchronized block is not recommended to declare entire method as synchronized if very few lines of code causes the problem that code we can declare inside synchronized block. So that we can improve the performance of the system.

Syntax:
synchronized(b)
{
//critical section.
}
Where ‘b’ is an object reference.

To get the lock for the current object we can define synchronized block as follows

synchronized(this)
{
//critical section.
}


4) What is the purpose of Synchronized block?

Ans:  Synchronized block is used to lock an object for any shared resource. Scope of synchronized block is smaller than the method.

5) What is Synchronized statement?

Ans:  The statement which are defined inside synchronized method or blocks are called synchronized statement.

6) What is Daemon Thread?

Ans: The threads which are running in the background to provide support for user defined threads are called 'Daemon Thread'.
We can check whether the given thread is daemon or not by using the following thread class thread.

public boolean isDaemon()

we can change the daemon nature of a thread by using setDaemon() method of thread class.

public void setDaemon(Boolean b)

7) What is DeadLock?

Ans: If two threads are waiting for each other forever then the threads are said to be in "deadlock" There is no deadlock resolution technique but prevention technique is available.

8) What is Thread Priority?

Ans: Every Thread in java having some priority. The range of valid thread priorities is (1-10) (1 is least & 10 isHighest). 

Thread class defines the following constant for representing some standard priorities.
Thread.MIN_PRIORITY ->1
Thread.NORM_PRIORITY ->2
Thread.MAX_PRIORITY >3

9) What Thread Scheduler?

Ans: Thread scheduler use these priorities while allocating C.P.U. The Thread which is having highest prioritywill get chance first for execution. If two threads having the same priority then which thread will get chancefirst for execution is decided by Thread Scheduler, which is vendor dependent i.e we can’t expect exactly.

The default priority for the main thread only the 5, but for all the remaining threads the priority will be inherit from parent i.e what ever the parent has the same priority the child thread also will get.



10) What is difference between notify() and notifyAll()?

Ans: notify() is used to unblock one waiting thread whereas notifyAll() method is used to unblock all the threads in waiting state.

11) How do you ensure that N threads can access N resources without deadlock?

Ans: A very simple way to avoid deadlock while using N threads is to impose an ordering on the locks and force each thread to follow that ordering. Thus,if all threads lock and unlock the mutexes in the same order,no deadlocks can arise.

12) What is the meaning by multi-threaded program?

Ans: A multithreaded  program contains two or more parts that can run concurrently.Each part of such a program is called a thread,and each thread defines a separate path of execution.

13) What is difference between yield() and sleep() methods?

Ans: The thread which is called yield() method temporarily pause the execution to give the chance for remaining threads of same priority. If there is no waiting thread or all waiting threads having low priority. Then the same thread will get the chance immediately for the execution.

If a method has to wait some predefined amount of time with out execution then we should go for sleep() method.

The main difference is when a task invokes its yield() method it returns to the ready state. When a task invokes its sleep() method,it returns to the waiting state.

14) What is difference between the methods sleep() and wait()?

Ans: The code sleep(2000); puts thread aside for exactly two seconds. The code wait(2000),causes a wait of up to two seconds. A thread could stop waiting earlier if it receives the notify() or notifyAll() call. The method wait() is defined in the class object and the method sleep() is defined in the class Thread.

15) What is an object's lock and which object's have locks?

Ans: An object lock is a mechanism that is used by multiple threads to obtain synchronized access to the object. A thread may execute a synchronized method of an object only after it has acquired the object's lock.

Comments

Popular posts from this blog

Mockito interview Questions

1.       Question 1. What Is Mockito? Answer : Mockito allows creation of mock object for the purpose of Test Driven Development and Behavior Driven development. Unlike creating actual object, Mockito allows creation of fake object (external dependencies) which allows it to give consistent results to a given invocation. 2.       Question 2. Why Do We Need Mockito? What Are The Advantages? Answer : Mockito differentiates itself from the other testing framework by removing the expectation beforehand. So, by doing this, it reduces the coupling. Most of the testing framework works on the "expect-run-verify". Mockito allows it to make it "run-verify" framework. Mockito also provides annotation which allows to reduce the boilerplate code. 3.       Question 3. Can You Explain A Mockito Framework? Answer : In Mockito, you always check a particular class. The dependency in that class is injected using mock object. So, for example, if you have service class

JAVA Expert Interview Questions Answers 2017

Java Basics ::  Interview Questions and Answers Home  »  Interview Questions  »  Technical Interview  »  Java Basics  » Interview Questions 1.     What is the difference between a constructor and a method? A constructor is a member function of a class that is used to create objects of that class. It has the same name as the class itself, has no return type, and is invoked using the new operator. A method is an ordinary member function of a class. It has its own name, a return type (which may be void), and is invoked using the dot operator. 2.     What is the purpose of garbage collection in Java, and when is it used? The purpose of garbage collection is to identify and discard objects that are no longer needed by a program so that their resources can be reclaimed and reused. A Java object is subject to garbage collection when it becomes unreachable to the program in which it is used. 3.     Describe synchronization in respect to multithreading. With respect to

Java Example Program to Convert List to Set

import java.util.ArrayList; import java.util.HashSet; import java.util.Set; public class ListToSet {  /**   * @author Amarjit Kumar   * @category interview questions   *   * Description: Convert List to set in java with example program   *   */  public static void main(String[] args) {   ArrayList<String> arrList= new ArrayList<>();   arrList.add("Java");   arrList.add("Java");   arrList.add("List to String");   arrList.add("Example Program");   Set<String> strSet = new HashSet<String>(arrList);   System.out.println(strSet);   System.out.println(arrList);  } } /*  * Java program to convert list to set. Convert ArrayList of string to HashSet  * in java example program How to convert List to Set in java Set<String> strSet  * = new HashSet<String>(arrList); HashSet having a constructor which will take  * list as an argument. Lets see how to convert list to Set using java program.