Daemon thread

Threads which are running in background to provide support to “main” thread are called Daemon thread.

Daemon thread example: Garbage collector, signal dispatcher etc.

When main thread runs with low memory problem then JVM runs Garbage collector to destroy useless objects. So that memory get improve and main thread can continue.

 

How to check thread is Daemon or not?

Below method is use to check current thread is Daemon or not.

isDaemon() method returns boolean value. IF current thread is not Daemon then it will return false.

Output:

As current working thread is “main” which is always non-daemon. That’s why false is answer.

 

How to set a thread as Daemon

We can change nature of thread using below method.

We can set non-Daemon thread to daemon and daemon to non daemon. But changing nature of a thread is possible before starting of a thread only.

Output:

If we try to change nature of a thread after starting we will get Runtime Exception: IllegalThreadStateException.
We can not change nature of “main” thread because it is already started by JVM.

Output:

Default nature of Thread

By default main thread is always non-daemon and all remaining threads nature will inherited from parent to child. That means if parent is daemon, child thread will also be daemon.

Output:

Whenever last non-Daemon thread terminates automatically all daemon thread will terminate irrespective of there position.

Help others by sharing the content!

Inter Thread communication

Two threads can communicate with each other by using following three methods. These methods are present in Object class but not in Thread class.

  • wait()
  • notify()
  • notifyAll()

wait(): The thread which is expecting a update is responsible to call wait() method. Then immediately the thread enter into waiting state.

notify(): The thread which will perform update is responsible to call notify() method. Then waiting thread will get notification and can continue with updated items.

notifyAll(): This method is used to give notification to all waiting threads.
 
Important points to remember

  • All these 3 methods wait(), notify(), notifyAll() are present in Object class but not in Thread class.
  • To call these methods on any object Thread should be owner of that object i.e thread should have lock of that object i.e thread should be in synchronized area. Otherwise will get Runtime exception: IllegalMonitorException.
  • If a thread call wait() method it immediately releases lock of that object and enter into waiting state.
  • If a thread call notify() method on any object it may not releases lock of that object immediately.
  • Except wait(), notify(), notifyAll() there is no other method where thread releases a lock.

 

Difference between notify() and notifyAll()

notify() will give notification to only one waiting thread. If multiple threads are waiting then only one thread will be notify and the remaining threads will have to wait until further notification.

notifyAll() is use to give notification to all waiting threads of a particular object. Even tho multiple threads get notification only one thread can operate on object at a time.

 

What is Deadlock

If two thread are waiting for each other forever such type of infinite waiting is called Deadlock.
Long waiting of thread where waiting will not end.

 

What is starvation

Long waiting of thread where waiting may end is call starvation.
Example: If there are 100 thread. One thread out of 100 have priority-1 other 99 has priority-10. Then thread with priority-1 has to wait to complete execution of all 99 thread. This waiting is going to long but will end.

Help others by sharing the content!

Synchronization

What is Synchronized keyword

If a multiple threads want to operate on a same object simultaneously, there may be problem of data inconsistency. To overcome this problem we can use synchronized keyword.

Synchronized keyword is only applicable to methods & blocks and not to variables & class.

 

Advantage and disadvantage of synchronized keyword / synchronization

Advantage: Using Synchronized keyword it reduces problem of data inconsistency.

Disadvantage: It increase waiting time of thread. At a time only one thread can operate on object so other threads have to wait. So it creates performance problem.

 

What is Race condition

If a multiple threads want to operate on a same object simultaneously. There may be problem of data inconsistency. This condition is called Race condition.

 

How synchronization is implemented? / Synchronized method

Internally synchronization is implemented by using lock. In java every object has a unique lock.

If a thread wants to operate any synchronization method on object then it first has to take lock of that object. Once thread got a lock then it is allowed to perform any synchronization method on that object.

Once thread completes execution it releases lock. The process of locking and releasing lock is internally take care by JVM.

lock concept is implemented based on object and not on methods.

 

Difference between class level lock and object level lock

What is class level lock

If a thread wants to execute a static synchronized method then first thread require class level lock. Once thread got class level lock then thread can perform any static synchronized method of that class.

 

What is object level lock

If a thread wants to execute a instance synchronized method then first thread require object level lock.

 

Synchronized block

If a very few line of code require a synchronization then it is not useful/ recommended to synchronization method.We should use synchronization block.

Synchronization block reduces waiting time of thread and increase performance as compare to synchronized method.

 

How to implement Synchronized block

We can implement Synchronized block by using following:

  • to get lock of current object – eg: synchronized(this)
  • to get lock of particular object
  • to get lock of class level object – eg: synchronized(Test.class) // Test is class name
Help others by sharing the content!