Thread synchronization, object level locking and class level locking
Synchronization refers to multi-threading. A synchronized block of code can only be executed by one thread at a time.
Java supports multiple threads to be executed. This may cause two or more threads to access the same fields or objects. Synchronization is a process which keeps all concurrent threads in execution to be in synch. Synchronization avoids memory consistence errors caused due to inconsistent view of shared memory. When a method is declared as synchronized; the thread holds the monitor for that method’s object If another thread is executing the synchronized method, your thread is blocked until that thread releases the monitor.
Synchronization in java is achieved using synchronized keyword. You can use synchronized keyword in your class on defined methods or blocks. Keyword can not be used with variables or attributes in class definition.
Object level locking
Object level locking is mechanism when you want to synchronize a non-static method or non-static code block such that only one thread will be able to execute the code block on given instance of the class. This should always be done to make instance level data thread safe. This can be done as below :
01.
public
class
DemoClass
02.
{
03.
public
synchronized
void
demoMethod(){}
04.
}
05.
06.
or
07.
08.
public
class
DemoClass
09.
{
10.
public
void
demoMethod(){
11.
synchronized
(
this
)
12.
{
13.
//other thread safe code
14.
}
15.
}
16.
}
17.
18.
or
19.
20.
public
class
DemoClass
21.
{
22.
private
final
Object lock =
new
Object();
23.
public
void
demoMethod(){
24.
synchronized
(lock)
25.
{
26.
//other thread safe code
27.
}
28.
}
29.
}
Class level locking
Class level locking prevents multiple threads to enter in synchronized block in any of all available instances on runtime. This means if in runtime there are 100 instances of DemoClass, then only one thread will be able to execute demoMethod() in any one of instance at a time, and all other instances will be locked for other threads. This should always be done to make static data thread safe.
01.
public
class
DemoClass
02.
{
03.
public
synchronized
static
void
demoMethod(){}
04.
}
05.
06.
or
07.
08.
public
class
DemoClass
09.
{
10.
public
void
demoMethod(){
11.
synchronized
(DemoClass.
class
)
12.
{
13.
//other thread safe code
14.
}
15.
}
16.
}
17.
18.
or
19.
20.
public
class
DemoClass
21.
{
22.
private
final
static
Object lock =
new
Object();
23.
public
void
demoMethod(){
24.
synchronized
(lock)
25.
{
26.
//other thread safe code
27.
}
28.
}
29.
}
Some Important notes
- Synchronization in java guarantees that no two threads can execute a synchronized method which requires same lock simultaneously or concurrently.
- synchronized keyword can be used only with methods and code blocks. These methods or blocks can be static or non-static both.
- When ever a thread enters into java synchronized method or block it acquires a lock and whenever it leaves java synchronized method or block it releases the lock. Lock is released even if thread leaves synchronized method after completion or due to any Error or Exception.
- java synchronized keyword is re-entrant in nature it means if a java synchronized method calls another synchronized method which requires same lock then current thread which is holding lock can enter into that method without acquiring lock.
- Java Synchronization will throw NullPointerException if object used in java synchronized block is null. For example, in above code sample if lock is initialized as null, the synchronized (lock) will throw NullPointerException.
- Synchronized methods in Java put a performance cost on your application. So use synchronization when it is absolutely required. Also, consider using synchronized code blocks for synchronizing only critical section of your code.
- It’s possible that both static synchronized and non static synchronized method can run simultaneously or concurrently because they lock on different object.
- According to the Java language specification you can not use java synchronized keyword with constructor it’s illegal and result in compilation error.
- Do not synchronize on non final field on synchronized block in Java. because reference of non final field may change any time and then different thread might synchronizing on different objects i.e. no synchronization at all. Best is to use String class, which is already immutable and declared final.
Happy Learning !!
No comments:
Post a Comment