Java basics: a comprehensive understanding of synchronized static keyword

Java synchronized keyword category belongs to knowledge-based, but in the Android framework layer of the source code, are extremely common.
Master it, it is the better reading, learning premise Android source code.

A basic knowledge

1. Role

Guarantee the same time at most one thread executes Synchronized modified method / code.

(Ie: other threads must wait to execute the method / code block after the process / thread currently executing the code block)

2. scenarios

Ensure security thread, multi-thread synchronization problem solving complicated by the (implementation is complicated by obstructive), specific scenario is as follows:

  • When the modified example of the method / code block (sync) is protected with an object method calls the current instance of the object &
  • When modifying static method / code block (sync) it is protected static method call & class class object

3. Grammar

synchronized (obj){
      .....
     //此处的代码就是同步代码块   
}

After the above syntax in synchronized parentheses obj is the meaning of the synchronization monitors, the above code is: thread starts executing synchronized block before, must first obtain a lock on the synchronization of the monitor.

Note:
(1) at any time, only one thread lock can obtain synchronization monitors, but the synchronization code block execution is finished, the thread will release the lock on the synchronization of the monitor.

(2) synchronized keywords modification method, code blocks can be modified, but not modified constructor, member variables.

II. Use Rules

Use the synchronized code blocks may be used in the method and in the Java code, the position Synchronized with these may be used:

Here Insert Picture Description
As shown, may be used in the synchronized method on may be used in the block , where the method is a static method and the example method is to lock each object instance of the object class and the class. May be divided into three types used in the code block, concrete can be seen in the table above. It should be noted here is: if the lock is a class object, then, despite the new multiple instances of objects, but they still belong to the same class will still be locked, that is, to ensure synchronization relationship between threads.

In Java, although the program allows to use any object as a synchronization monitor, but think about the purpose of synchronizing the monitor: two threads to prevent concurrent access to the same shared resource, because normally recommended might be act as synchronization of concurrent access to shared resources monitor.

Three performance tips:

Thread-safe class of variable efficiency of the program is to reduce operating expense, in order to reduce the negative impacts caused by thread-safe, the program can take the following strategies:

(1) Do not thread-safe class for all methods are synchronized, only those methods will change the competitive resources (that is, competition for resources shared resources) are synchronized.

(2) if there are two categories of variable operating environment: single-threaded environment and multi-threaded environment, it should provide two versions for variable categories, namely thread-safe version and thread-safe versions use threads in a single-threaded environment. unsafe version to ensure performance, thread-safe version in a multithreaded environment.

JDK provided StringBuilder, StringBuffer class is to take care of single-threaded and multi-threaded environments provided in a single-threaded environment, you should use StringBuilder to ensure better performance; when you need to ensure multi-thread safe, you should use StringBuffer.

Published 81 original articles · won praise 37 · views 50000 +

Guess you like

Origin blog.csdn.net/gaolh89/article/details/94352986