Java road to the white --java concurrent programming of synchronized beginner

synchronized beginner

Summing up (what)

Contact with java development has been nearly a year, it has been simply use the framework provided by the company, according to a fixed model to achieve business functions. Thinking back, I want to go further on java development, so first of all want to understand and grasp the java concurrent programming

Want to learn concurrent programming, even if I do not understand is aware of the synchronized keyword, its role is to control multiple concurrent threads to access shared resources security issues, gather information learned through:

1. When synchronized with a sync block of two or more threads access the same object, there is only a change to the thread resources to compete executed, another thread must wait until the completion of the currently executing thread continues to change synchronization competition execution right code block;

2. When one thread is accessing a target object of a synchronized block synchronized, other threads will access the synchronized code blocks all other target object is blocked;

3. A thread can get when performing tasks repeatedly the same object lock, such as: a thread synchronization method call a method on an object, and the synchronization method in turn invokes another method of synchronization objects of the object itself. In this case, the number of tracks JVM object locked, once an object is unlocked, the lock-1 number of objects, every time when each task to acquire a lock on the object, the number of +1 the lock; (may at the beginning of the competition to know that only a successful lock-threaded task order won several locks), because whenever leaving the task once synchronized method, the number of locks on the minus 1, gradually decreasing, the number until the lock becomes 0, the resource is full release, other tasks can begin to compete for this resource;

When to use (when)

 Brian synchronization rules: If you are "write" a variable that might be followed by another thread will be "read" take or are "read" take a last another thread is already "written" over variables, then you you must use synchronization, and "read" / "write" threads must be synchronized with the same monitor lock.

Note: Each access to shared resources critical methods must be synchronized, otherwise they will not work correctly.

How to use (how)

synchronized keyword, comprising two uses: synchronized method and synchronized block .
  • synchronized 方法:
public synchronized void countNum(int n){}

All methods synchronized particular object share the same lock, this mechanism ensures that the same timing for each instance of a class, member function declarations all synchronized in at most one in an executable state (as can be obtained at most one instance of the class corresponding to lock), so as to effectively avoid the access Violation class member variables (as long as all possible methods to access class member variables are declared as synchronized). 

  Not only that, static methods can be declared as synchronized, to control their access to static member variables of the class.

public static synchronized void countNum(int n){}

 

Defects synchronized method: If a big statement for the synchronized method will greatly affect the efficiency.

  Typically, if the method of the thread class run () declared as synchronized, as it has been in operation throughout the lifetime of the thread, so that it will lead to any call of this class are synchronized method will never succeed. Of course, we can code access class member variables into a special method, which is declared as synchronized, and the call to solve the problem in the main method, but Java provides a better solution for us, and that is synchronized block.

  • synchronized 块:
the synchronized (SyncObject.Class) { 
     // code allows the access control 
}

May also be written in the following format, this-- refers to the current class

the synchronized ( the this ) { 
     // allow access control code 
}

Lock synchronized block code must be subject SyncObject (as described above, can be a class instance or class) in order to perform, the particular mechanism supra. Since for any block of code, and may be arbitrarily specified object locked, so the higher flexibility.

  When using synchronized blocks, be sure to follow Brian synchronization rules, and each method of accessing a critical shared resources are synchronized.

 

reference:

https://blog.csdn.net/yuan1013922969/article/details/54971632

 

Guess you like

Origin www.cnblogs.com/EtherealWind/p/11131264.html