Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the three elements of Java concurrent programming

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the knowledge of "what are the three elements of Java concurrent programming". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1 atomicity 1.1 definition of atomicity

Atomicity refers to one or more operations that are either performed and not interrupted by other operations, or none of them are performed.

1.2 causes of atomicity problems

Thread switching is the cause of atomicity problems, and thread switching is to improve the utilization of CPU.

In the case of count + +, at least three CPU instructions are required:

Instruction 1: first, you need to load the variable count from memory into the register of CPU

Instruction 2: after that, perform the + 1 operation in the register

Instruction 3: finally, the result is written to memory (the caching mechanism makes it possible to write to the CPU cache rather than memory).

Let's assume that count=0, if thread A makes a thread switch after instruction 1 is executed, and thread An and thread B execute according to the sequence shown in the figure below, then we will find that both threads have performed the operation of count+=1, but the result is not the 2 we expected, but 1.

1.3 Atomic operation

In a multithreaded environment, Java only ensures that variables and assignments of basic data types are atomic (Note: in a 32-bit JDK environment, reading 64-bit data is not an atomic operation *, such as long, double)

1.4 how to solve the atomicity problem

If we can guarantee that changes to shared variables are mutually exclusive, then atomicity can be guaranteed for both single-core CPU and multi-core CPU. Locking can solve atomicity problems, such as using synchronized and lock.

2 visibility 2.1 visibility definition

Visibility means that when multiple threads operate on a shared variable, after one thread modifies the variable, the other threads can immediately see the results of the modification.

2.2 causes of visibility issu

The data consistency between CPU cache and memory is the cause of visibility problems, and CPU cache is to improve the efficiency of CPU.

2.3 visibility issues Resolution

The cause of the visibility problem is CPU caching, so we can just disable CPU caching.

The volatile field can disable CPU caching and resolve visibility issues.

Both synchronized and locks guarantee visibility.

2.4 what are the visibility rules

The visibility rule is the Happens-Before rule.

Happens-Before rules:

To put it simply: the result of the previous operation is visible to the subsequent operation.

Happens-Before restricts the optimization behavior of the compiler. Although it allows the compiler to optimize, it requires the compiler to comply with the Happens-Before rules after optimization.

2.5 Happens-Before rules

The sequence rules of the program

In a thread, the previous operation Happens-Before any subsequent operation in program order.

Class Example {public void test () {int x = 42; ① int y = 20; ②}}

① Happens-Before ② .

Volatile variable rule

For write operations on a volatile variable, Happens-Before follows subsequent read operations on the volatile variable.

Transitivity rule

If A Happens-Before B, and B Happens-Before C, then A Happens-Before C.

Class Example {int x = 0; volatile int y = 0; public void writer () {x = 42; ① y = 1; ②} public void reader () {if (y = = 1) {③ / / how much would x be here? }}}

① Happens-Before ②, satisfying rule 1-sequentiality rules.

② Happens-Before ③, meeting the rule 2-volatile variable rule.

① Happens-Before ③, satisfying rule 3-transitivity rules. If y = = 1, then x = 42

Rules of locking in pipe process

The Happens-Before of unlocking a lock is followed by locking the lock.

Pipe program is a general synchronization primitive, which means that synchronized,synchronized is the implementation of pipe program in Java in Java.

Synchronized (this) {/ / automatic locking here / / x is a shared variable, initial value = 10 if (this.x < 12) {this.x = 12;}} / / automatically unlocked here

Assuming that the initial value of x is 10, the value of x becomes 12 after thread An executes the block of code (automatically releases the lock after execution)

When thread B enters the code block, you can see the write operation of thread A to x, that is, thread B can see xwriter 12.

Thread start () rule

It means that after the main thread A promoter thread B, child thread B can see the operation of the main thread in front of the promoter thread B.

Thread join () rule

It means that the main thread A waits for child thread B to complete (main thread An is realized by calling the join () method of child thread B). When child thread B completes (the join () method in main thread A returns), the main thread can see the operation of the child thread. Of course, the so-called "see" refers to the operation of shared variables.

3orderliness 3.1 definition of orderliness

Orderliness, that is, the order in which the program is executed according to the order of the code.

3.2 causes of orderliness problems

In order to optimize performance, the compiler sometimes changes the order of statements in the program.

For example, the compiler may be optimized to change the order of statements but does not affect the final result of the program.

Take the double check code as an example:

Public class Singleton {static Singleton instance; static Singleton getInstance () {if (instance = = null) {① synchronized (Singleton.class) {if (instance = = null) instance = new Singleton (); ②}} return instance;}}

There is a problem with the above code, but the problem lies in the ② operation: the optimized execution path is as follows:

Allocate a piece of memory M

Assign the address of M to the instance variable

Finally, the Singleton object is initialized on memory M.

What problems will be caused by optimization? We assume that thread An executes the getInstance () method first, and the thread switch happens to occur when the ① is finished, switching to thread B; if thread B also executes the getInstance () method, thread B will find instance! = null when executing the first judgment, so it will directly return instance, while the instance at this time is not initialized, if we access the member variables of instance at this time, a null pointer exception may be triggered.

How to solve the problem of double checking? Variables are modified with volatile to prevent instruction reordering.

Public class Singleton {static volatile Singleton instance; static Singleton getInstance () {if (instance = = null) {① synchronized (Singleton.class) {if (instance = = null) instance = new Singleton (); ②}} return instance;}} "what are the three elements of Java concurrent programming". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report