Thursday, February 16, 2017

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.

Recommended to Read More Java Interview Questions:

Java Security Interview Questions
Java Design Pattern Interview Questions
Java OOPS Interview Questions
Top 10 Programmer Mistakes in Coding

1 comment:

  1. Your blog has given me that thing which I never expect to get from all over the websites. Nice post guys!

    SEO Melbourne

    ReplyDelete

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...