Monday, October 8, 2018

Important points of synchronized keyword in Java



1. The keyword synchronized in Java is used to provide mutually exclusive access to a shared resource with multiple chaining in Java. Synchronization in Java ensures that no two segments can execute a synchronized method that requires the same lock simultaneously or simultaneously.

2. You can use the synchronized keyword java only in the synchronized method or in the synchronized block.

3. Every time a chaining enters the synchronized java or blocks method, it acquires a block and whenever it leaves the synchronized method java or blocks, it releases the block. Blocking is released even if the thread leaves the method synchronized after completing or due to any error or exception.

4. The Java segment acquires an object level lock when it enters a synchronized instance java method and acquires a class level lock when it enters the static synchronized java method.

5. Java synchronized keyword is reentrant in nature means that if a synchronized java method calls another synchronized method that requires the same locking, then the current segment that is holding the lock can enter that method without acquiring the lock.

6. Java Synchronization will throw NullPointerException if the object used in the java synchronized block is null, for example, synchronized (myInstance) will throw java.lang.NullPointerException if myInstance is null.

7. A major disadvantage of the synchronized Java keyword is that it does not allow simultaneous reading, which can potentially limit scalability. With the concept of removing locks and using different read and write locks, you can overcome this synchronization limitation in Java. You will be happy to know that java.util.concurrent.locks. ReentrantReadWriteLock provides the implementation of ReadWriteLock in Java.

8. One more limitation of the synchronized Java keyword is that it can only be used to control access to a shared object within the same JVM. If you have more than one JVM and you need to synchronize access to a file system or a shared database, the synchronized Java keyword will not be sufficient. It is necessary to implement a type of global blockade for it.

9. The synchronized keyword of Java incurs a cost of performance. A synchronized method in Java is very slow and can degrade performance. Therefore, use java synchronization when absolutely necessary and consider using the synchronized java block to synchronize only the critical section.

10. The block synchronized with Java is better than the method synchronized with Java in Java because when using the synchronized block, you can only block the critical section of the code and avoid blocking the entire method that can degrade performance. A good example of Java synchronization around this concept is to obtain the Singleton class of the Instance () method. 



11. It is possible that both static synchronized and non-synchronized static methods can be executed simultaneously or simultaneously because they block the different object.

Starting on Java 5 after a change in the Java memory model, readings and recordings are atomic for all variables declared using the volatile keyword (including long and double variables) and simple variable atomic access is more efficient in instead of accessing these variables through synchronized java code. But it requires more care and attention from the programmer to avoid memory consistency errors.

13. The code synchronized with Java can result in deadlock or starvation when accessing multiple chaining if the synchronization is not implemented correctly. To know how to avoid deadlock in java, see here.

14. According to the Java language specification, you cannot use the synchronized Java keyword with the constructor that is illegal and produce compilation errors. Therefore, it is not possible to synchronize the constructor in Java, which seems logical, because other chaining cannot see the object being created until the chaining that created it has completed it.

15. You cannot apply java synchronized keywords with variables and can not use the volatile java keyword with the method.

16. Java.util.concurrent.locks extends the resource provided by the synchronized Java keyword to write more sophisticated programs, as they offer more resources, for example. Re-entry and interruptible blocks.

17. The Java synchronized keyword also synchronizes memory. Actually, the synchronized java synchronizes all the memory of the subprocess with the main memory.


18. Important methods related to synchronization in Java are wait (), notify () and notifyAll () that are defined in the Object class. You know why they are defined in the java.lang.object class instead of java.lang.Thread? You can find some reasons, which makes sense.

19. Do not synchronize in the non-final field in the synchronized block in Java. because the reference of the non-final field can change at any time and then the different thread can synchronize on different objects, that is, no synchronization at all. an example of synchronization in the non-final field:

private String lock = new String ("lock");
synchronized (blocking) {
System.out.println ("lock in:" + lock);
}

anyone if you write synchronized code like above in java can receive a warning "Synchronization in the non-final field" in IDE such as NetBeans and InteliJ

20. It is not recommended to use the String object as a block in the synchronized java block, because a string is an immutable object and a literal string and an interned string are stored in the string group. then, by chance, if any other part of the code or any third-party library used the same string that was blocked there, both are locked in the same object despite not being completely related, which could result in unexpected behavior and bad performance . instead of the String object, it is advisable to use the new object () for synchronization in Java in the synchronized block.

static end private String LOCK = "lock"; // not recommended
private static final Object OBJ_LOCK = new Object (); // Best

public void proces () {
synchronized (LOCK) {
........
}
}

21. In the Java library, the Calendar and SimpleDateFormat classes are not thread safe and require external synchronization in Java for use in the multi-tenancy environment.

And finally,

Probably, the most important point about synchronization in Java is that, in the absence of synchronized keywords or another construction, for example. Volatile variable or atomic variable, compiler, JVM and hardware are free to optimize, suppose, reorder or cache code and data, which can cause subtle competition errors in the code. Introducing the synchronization using volatile, atomic or synchronized variables, we instruct the compiler and the JVM not to do so.





No comments:

Post a Comment

From Java 8 to Java 11

Switching from Java 8 to Java 11 is more complicated than most updates. Here are some of my notes on the process. Modules Java 9 i...