Multi-thread learning,

Source: Internet
Author: User

Multi-thread learning,

Original address http://www.cnblogs.com/xrq730/p/4853578.html

Sleep () does not release the lock, join () releases the lock

The construction method and static block of the thread class are called by the main thread, while the run () method of the thread class is called by the application thread.

Class lock and method lock

KeywordsThe locks obtained by synchronized are all Object locks, instead of using a piece of code or method (function) as a lock.Which thread executes the method with the synchronized keyword first, and which thread willHold the lock of the object to which this method belongsOther threads can only wait. But there is a premise: Since the lock is called an object lock, it is bound to be related to the object, soMultiple Threads must access the same object..

1,Thread A holds the Lock of the Object. Thread B can call non-synchronized methods in the Object asynchronously.

2,Thread A holds the Lock of the Object. If thread B calls the synchronized Method in the Object at this time, it needs to wait, that is, synchronize

The meaning of the lock re-entry is:After a thread obtains an object lock, it can obtain the object lock again when requesting the Lock of this object again.

Static Synchronization Methods and non-static Synchronization Methods hold different locks. The former is a class lock, and the latter is an object lock..

The so-called class lock, for example. Assume that there is A static synchronization method A in A class, and instance B and instance C are new. Thread D Holds instance B, and thread E holds instance C, as long as thread D calls the method, thread E must wait until thread D finishes executing the method, although the two threads hold different objects.

Atomicity

An atomic operation indicates that an operation is inseparable and no other thread can interrupt or check the variables in an atomic operation. An atomic class is a class available for atomic operations. It can ensure thread security without locking.

However, this thread security is not absolute, and the output results are also random in the case of logic.

Volatile

Java has a primary memory, and different threads have their own working memory. The same variable value has a portion in the primary memory. If the thread uses this variable, there is an identical copy in your working memory. Each time a thread enters the main memory, the variable value is obtained from the main memory, and the variable is synchronized from the working memory to the main memory after each execution.

Volatile is used to ensure that the variable modified by volatile is the latest value each time it is read. Thread security focuses onVisibilityAndAtomicityThese two features are expanded,Volatile solves the visibility of variables between multiple threads, but it cannot guarantee atomicity..

In addition to ensuring atomicity, synchronized also ensures visibility. Because synchronized will first copy data from the primary memory to the working memory for both synchronous methods and synchronized code blocks. After the synchronization code block ends, the data in the working memory will be updated to the primary memory, in this way, the data in the main memory must be up-to-date.

A single atomic operation is thread-safe, but the combination of multiple atomic operations may be non-thread-safe, and the fail-fast mechanism is also caused by this. In fact, this has a special term in Java: relative thread security.



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.