JVM-concurrency-thread security and lock optimization, jvm-concurrent threads

Source: Internet
Author: User

JVM-concurrency-thread security and lock optimization, jvm-concurrent threads
Thread security and lock Optimization

1. Thread Security
(1) When multiple threads access an object, if the scheduling and alternate execution of these threads in the execution environment are not considered, no additional synchronization is required, or when the caller performs any other coordination operations and the behavior of calling this object can obtain correct results, the object is thread-safe.

(2) thread security in Java

A) data shared by various operations in the Java language can be divided into five categories: immutable, absolute thread security, relative thread security, thread compatibility, and thread opposition.

B) immutable objects in the Java language must be thread-safe. No thread security measures are required for implementation of object methods or callers.

C) thread compatibility: the object itself is not thread-safe, but it can be safely used in the concurrent environment by correctly using Synchronization Methods on the caller.

D) thread opposition: no matter whether the called end has taken synchronization measures, Code cannot be concurrently used in a multi-threaded environment.

(3) Implementation of thread security

A) mutex synchronization is a common guarantee of concurrency correctness. Synchronization means that when multiple threads concurrently access Shared data, the shared data can only be used by one thread at a time. Mutex is a means to achieve synchronization. The critical section, mutex volume, and semaphores are the main mutex implementation methods.

B) in Java, the most basic mutex synchronization method is the synchronized keyword. After the synchronized keyword is compiled, the two bytecode commands monitorenter and monitorexit are formed before and after the code block is synchronized, both bytecode require a reference parameter to specify the object to be locked and unlocked.

C) the synchronized synchronization block can be reentrant to the same thread without locking itself. The synchronization code block blocks other threads before it is executed.

D) In addition to synchronized, you can also use ReentrantLock in the java. uitl. concurrent package for synchronization.

E) compared with synchronized, ReentrantLock provides some advanced features: Waiting for interruption. Fair locks can be implemented, and locks can be bound to multiple conditions.

Waiting for interruption: When the lock-holding thread does not release the lock for a long time, the waiting thread can choose to give up waiting and change to other tasks, the resumable feature is helpful for processing synchronization blocks that take a long time.

Fair lock: when multiple threads are waiting for a lock, they must obtain the lock in sequence according to the time sequence of applying for the lock.

F) non-blocking synchronization: An Optimistic Concurrency policy based on conflict detection. That is to say, operations are performed first. If no other threads compete for shared data, the operation is successful. If the shared data is in competition, if there is a conflict, then other compensation measures will be taken. Many implementations of this optimistic concurrency policy do not need to suspend the thread.

G) No synchronization Solution
Code reentrant: Also called pure code, can interrupt the code at any time during code execution and execute another piece of code. After the return of control, the original program will not have any errors.
Local thread storage.

2. Lock Optimization
(1) spin lock and adaptive spin
To let the thread wait, you only need to let the thread execute a busy loop (spin). This technology is the spin lock.
(2) Lock Elimination
This means that the real-time compiler of a virtual machine requires synchronization of some code at runtime, but is detected that there is no possibility of a lock competing with shared data to be eliminated. The primary cause of lock elimination is the escape analysis data.
(3) Lock roughening
Extend (roughening) the range of synchronization locks
(4) Lightweight lock
(5) biased lock

Related Article

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.