"Java Concurrency Programming Practical" Learning notes

Source: Internet
Author: User

Second, thread safety

Correctness:

The behavior of a class is exactly the same as its specification.

Thread Safety:

This class is thread-safe when multiple threads access a class, regardless of how the runtime environment is scheduled or how those threads will be executed alternately, and if no additional synchronization or collaboration is required in the Keynote code , the class can behave correctly.

Stateless Objects :

An object that contains neither a domain nor any reference to the domain in any other class.

Stateless objects must be thread-safe.

Race Condition:

A race condition occurs when the correctness of a calculation depends on the alternating execution timing of multiple threads.

The essence is to make judgments or perform a calculation based on an observation that may fail.

The most common type of race condition is "check first and then execute (check-then-act)".

The read-Modify-write operation is also a race condition.

Atomic nature:

Assuming that there are two operations A and B, if from the thread that executes a, when another thread executes B, either all of B is executed, or no B is executed at all, then A and B are atomic to each other.

Atomic operation:

For all operations that access the same state, including the operation itself, this operation is an atomic operation.

To maintain state consistency, you need to update all related state variables in a single atomic operation.

Built-in Lock:

Each Java object can be used as a lock that implements synchronization, which is called a built-in lock (intrinsic lock) or a monitor lock.

The built-in lock is a mutex, with at most one thread holding the lock.

The synchronization code block (Synchronized block) consists of two parts:

An object reference as a lock, a block of code that is protected by this lock.

The keyword synchronized adornment method is a kind of synchronous code block, the lock is the object that the method call resides, the static synchronized method takes the class object as the lock.

re-entry :

Because the built-in lock is reentrant, the request succeeds if a thread attempts to acquire a lock that is already held by itself.

Re-entry means that the operation granularity of acquiring a lock is "thread", not "call".

Implementation: Associates a Get count value and an owner thread for each lock. When the count value is 0 o'clock, the lock is considered not to be held by any thread. When a thread requests a lock that is not held, the JVM writes down the lock's holder and resets the get count value to 1. If the same thread acquires the lock again, the count value is incremented, and when the thread exits the synchronization code block, the value is reduced by 1.

The re-entry improves the encapsulation of the lock behavior, thus simplifying the execution of object-oriented concurrency code.

to protect the state with a lock:

Locks enable their protected objects to be executed in a serial manner.

For mutable state variables that may be accessed concurrently by multiple threads, the same lock is required to access it, in which case we call the state variable to be protected by this lock.

There is no intrinsic connection between an object's built-in lock and its state, although most classes use the built-in lock as an effective locking mechanism, but the object's domain is not necessarily protected by a built-in lock .

A common locking convention: All mutable states are encapsulated inside the object, and all access to the mutable state's code path is synchronized through the object's built-in lock, so that concurrent access does not occur on the object.

For each invariant condition that contains multiple variables, all of the variables involved need to be protected by the same lock .

Activity and Performance:

It is not good to decompose synchronized blocks of code too finely, because the acquisition and release of the lock operation requires overhead.

Do not hold a lock when the execution takes longer to calculate or when an operation may not be completed quickly, such as I/O.

Using two different synchronization mechanisms at the same time can lead to confusion and no performance or security benefits. (such as built-in lock synchronized and atomic atomic variables).

"Java Concurrency Programming Practical" Learning notes

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.