Java Multithreading: Thread sync with keyword synchronized

Source: Internet
Author: User

one, the characteristics of synchronization
1. You do not have to synchronize all the methods in the class, and the class can have both synchronous and non-synchronous methods.
2. If the thread has synchronous and non-synchronous methods, the non-synchronous method can be freely accessed by multiple threads without being restricted by the lock. See Experimental 1:http://blog.csdn.net/huang_xw/article/details/7318561
3. If two threads are executing a synchronous method in a class, and two threads use the same instance to invoke the method, only one thread can execute the method at a time, and the other waits until the lock is freed. See Experimental 2:http://blog.csdn.net/huang_xw/article/details/7318571
4. If two threads are executing a different synchronization method in a class, and two threads use the same instance to invoke the method, only one thread can execute the method at a time, and the other waits until the lock is freed. Because an instance object is a lock. See Experimental 3:http://blog.csdn.net/huang_xw/article/details/7318578
5. Not only class instances, each class also corresponds to a lock, so we can also declare the static member function of the class as synchronized to control its access to static member variables of the class. See Experimental 4:http://blog.csdn.net/huang_xw/article/details/7318588
second, the significance of synchronized
Synchronized is used for multi-threaded design, with the Synchronized keyword, the running results of multi-thread programming will become controllable. The Synchronized keyword is used to protect shared data.
three, the synchronized realization synchronization mechanism
Synchronized relies on "lock" mechanism for multi-threaded synchronization, "lock" there are 2 kinds, one isObject Lock, one isclass Lock.
iv. synchronized keywords can be decorated with objects
1. The modifier of the Synchronized{static method or Common method} function is the synchronous method.
2. synchronized{modifier code block} can be used as the modifier of a statement inside a function, that is, a synchronous statement block
3. synchronized{Object Reference} Note that the object must be thread-safe. such asStringBufferIn JDK1.5 is thread-safe, and arrays, ArrayList, are unsafe. See Experimental 5:http://blog.csdn.net/huang_xw/article/details/7318593
attached: Methods to resolve thread insecurity:
1. If the object is immutable, it is thread-safe, for example: String, which can be used with confidence.
2. If the object is thread-safe, for example: StringBuffer, use it with confidence.
3. Conditional thread safety, which is generally thread-safe for vectors and Hashtable, but for some special cases, requires additional synchronized to ensure thread safety.
4. Use the Synchronized keyword.

Java Multithreading: Thread sync with keyword synchronized

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.