|
JAVA Interview Questions 46 - 51
|
Back to the list of all Java Interview Questions
|
|
Question 46: What is the difference between yield and sleeping? What is the difference between the methods sleep() and wait()?
|
When a task invokes yield(), it changes from running state to runnable state. When a task invokes sleep(), it
changes from running state to waiting/sleeping state.
The method wait(1000), causes the current thread to sleep up to one second. A thread could sleep less than 1
second if it receives the notify() or notifyAll() method call. The call to sleep(1000) causes the current thread to sleep for exactly 1 second.
|
|
Question 47: How does thread synchronization occurs inside a monitor? What levels of synchronization can you apply? What is
the difference between synchronized method and synchronized block?
|
|
In Java programming, each object has a lock. A thread can acquire the lock for an object by using the
synchronized keyword. The synchronized keyword can be applied in method level (coarse grained lock – can
affect performance adversely) or block level of code (fine grained lock). Often using a lock on a method level is
too coarse. Why lock up a piece of code that does not access any shared resources by locking up an entire method. Since each object has a lock, dummy objects can be created to implement block level synchronization.
The block level is more efficient because it does not lock the whole method.
|
|
class
MethodLevel
{
//shared among threads
SharedResource x, y
;
pubic void
synchronized
method1()
{
//multiple threads
can't access
}
pubic void
synchronized
method2()
{
//multiple threads
can't access
}
public void method3()
{
//not synchronized
//multiple threads
can access
}
}
|
|
|
|
class
BlockLevel
{
//shared among threads
SharedResource x, y
;
//dummy objects for
locking
Object xLock = new
Object(), yLock = new Object();
pubic void method1()
{
synchronized(xLock)
{
//access x here. thread
safe
}
//do something here
but don't use SharedResource x, y
// because will not
be thread-safe
synchronized(xLock)
{
synchronized(yLock)
{
//access x,y here.
thread safe
}
}
//do something here
but don't use SharedResource x, y
//because will not
be thread-safe
}//end of method1
}
|
The JVM uses locks in conjunction with monitors. A monitor is basically a guardian who watches over a sequence
of synchronized code and making sure only one thread at a time executes a synchronized piece of code. Each
monitor is associated with an object reference. When a thread arrives at the first instruction in a block of code it
must obtain a lock on the referenced object. The thread is not allowed to execute the code until it obtains the lock.
Once it has obtained the lock, the thread enters the block of protected code. When the thread leaves the block, no
matter how it leaves the block, it releases the lock on the associated object.
|
|
Question 48: Why synchronization is important?
|
Without synchronization, it is possible for one thread to modify a shared
object while another thread is in the process of using or updating that object’s value. This often causes dirty data
and leads to significant errors. The disadvantage of synchronization is that it can cause deadlocks when two
threads are waiting on each other to do something. Also synchronized code has the overhead of acquiring lock,
which can adversely affect the performance.
|
|
Question 49: What is a ThreadLocal class?
|
ThreadLocal is a handy class for simplifying development of thread-safe
concurrent programs by making the object stored in this class not sharable between threads. ThreadLocal class
encapsulates non-thread-safe classes to be safely used in a multi-threaded environment and also allows you to
create per-thread-singleton.
|
|
Question 50: What is a daemon thread?
|
Daemon threads are sometimes called "service" or “background” threads. These are threads that normally run at a
low priority and provide a basic service to a program when activity on a machine is reduced. An example of a
daemon thread that is continuously running is the garbage collector thread. The JVM exits whenever all nondaemon
threads have completed, which means that all daemon threads are automatically stopped. To make a
thread as a daemon thread in Java -> myThread.setDaemon(true);
|
|
Question 51: How can threads communicate with each other? How would you implement a producer (one thread) and a consumer (another thread) passing data (via stack)?
|
|
The wait(), notify(), and notifyAll() methods are used to provide an efficient way for threads to communicate with
each other. This communication solves the ‘consumer-producer problem’. This problem occurs when the
producer thread is completing work that the other thread (consumer thread) will use.
|
Back to the list of all Java Interview Questions
|