The core technology of Java multithreaded Programming reading notes __ algorithm

Source: Internet
Author: User

Non-thread-safe actually occurs when multiple threads have concurrent access to an instance variable in the same object. The result is "dirty reading", that is, the data taken is actually changed. "Thread-safe" means that the value of the obtained instance variable is synchronized and no "dirty reads" occur.

"Non-threading security issues" exist in instance variables, and if they are private variables inside a method, there is no "thread-safe" problem

Two threads access synchronous methods of the same name for each of the two different instances of the same class, and the result is executed asynchronously. The lock acquired by the keyword synchronized is an object lock, rather than a piece of code or method (function) as a lock.

As a result, multiple threads access synchronization methods for the same object, and synchronized can guarantee synchronous execution. If you are synchronizing methods for multiple objects, you cannot guarantee synchronous execution

The method of synchronized the keyword must be queued, and the word "sharing" should be firmly remembered. Only read and write access to shared resources needs to be synchronized, and if it is not a shared resource, there is no need for synchronization at all.

A thread holds the lock lock of an object first, and a B-thread can call a method of a synchronized type in an object in an asynchronous manner.

A thread first holds the lock lock of an object, and a B thread needs to wait, that is, synchronization if it calls a method of type synchronized in object.

Synchronized re-entry
The keyword synchronized has the function of locking a lock, that is, when using synchronized, when a thread gets an object lock, it is possible to get the object lock again by requesting it again.

The concept of "reentrant locks" is that you can get your own internal locks again.
Reentrant locks are also supported in environments where parent-child classes inherit.
When a thread executes a code that has an exception, its held locks are automatically freed.
Synchronization does not have inheritance

Synchronized synchronization code block can improve the execution efficiency of the program, reduce the scope of synchronization

When multiple threads invoke a synchronized synchronous method or a synchronized (this) synchronized code block with a different name in the same object, the effect of the invocation is to execute sequentially, that is, synchronized blocking.

In fact, Java also supports the "arbitrary object" as the "object Monitor" to achieve synchronization function. Most of this "arbitrary object" is an instance variable and a parameter of the method, using the format synchronized (non-this object x)

The lock non-this object has certain advantages: if there are many synchronized methods in a class, this is even though it can be synchronized, but will be blocked, so it affects the efficiency of the operation. However, if you use a synchronized code block to lock a non-this object, the program in the code block and the synchronization method are asynchronous and do not scramble for this lock with the other lock this synchronization method can greatly increase your influence.
Synchronization occurs when multiple threads simultaneously perform synchronized (x) {} Synchronization code blocks.
Synchronous methods are synchronized when other threads perform the X object.
The synchronization effect is also rendered when other threads execute the synchronized (this) code block inside the X object method.
Note: If other threads call a method that does not add the Synchronized keyword, it is called asynchronously.

The keyword synchronized can also be applied to static statics methods. If you write this, it is a lock on the class class that corresponds to the current *.java file. Unlike previous object locks, which are class locks, the static synchronization method and the normal synchronization method are performed asynchronously because they hold different locks.

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.