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

How to achieve visibility in java multithreaded Synchronized

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

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you "java multithreaded Synchronized how to achieve visibility", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "java multithreaded Synchronized how to achieve visibility" this article.

Synchronized achieves visibility principle visibility

To achieve the visibility of shared variables, two things must be guaranteed:

The value of the shared variable modified by the thread can be refreshed from working memory to main memory in time.

Other threads can update the latest values of shared variables from main memory to their own working memory in time.

The implementation of visibility supported at the language level of Java

Synchronized

Volatile

Synchronized implements visibility

Synchronized can achieve:

Atomicity (synchronization)

Visibility

JMM's two provisions on synchronized:

1. Before the thread is unlocked, the latest value of the contribution variable must be refreshed to main memory.

two。 When a thread is locked, it clears the value of the shared variable in the working memory, so when using the shared variable, you need to re-read the latest value from the main memory (note: locking and unlocking require the same lock)

Changes to shared variables before the thread is unlocked are visible to other threads the next time the lock is added

The process by which a thread executes mutually exclusive code

Get mutex

Clear working memory

Copy the latest copy of the variable from main memory to working memory

Execute the code

Refresh the value of the changed shared variable to the main memory

Release mutex

Reorder:

The order in which the code is written is different from the order in which it is actually executed, and instruction reordering is optimized by the compiler or processor to improve program performance

Reordering of compiler optimization (compiler optimization)

Instruction-level parallel reordering (processor optimization)

Reordering of memory systems (processor optimization)

As-if-serial

As-if-serial: no matter how to reorder, the result of program execution should be consistent with the result of code sequence execution (Java compiler, runtime, and processor all guarantee that Java follows as-if-serial semantics in single thread)

Public class SynchronizedTest {/ * shared variable * / private boolean ready = false; private int result = 0; private int number = 1; / * write operation * / public void write () {ready = true;//1.1 number = 2 / / 1.2} / * read operation * / public void read () {if (ready) {/ / 2.1 result=number*3;//2.2} System.out.println ("value of result is:" + result) } / * * Internal thread class * / private class ReadWriteThread extends Thread {/ / determines whether the thread performs a read operation or a write operation private boolean flag; public ReadWriteThread (boolean flag) {this.flag=flag based on the flag parameter passed in the constructor } @ Override public void run () {if (flag) {/ / pass true in the constructor to perform the write operation write ();} else {/ / pass in false in the constructor to perform the read operation read () } public static void main (String [] args) {SynchronizedTest synchronizedTest = new SynchronizedTest (); / / start a thread to perform a write operation synchronizedTest.new ReadWriteThread (true). Start (); / / start a thread to perform a read operation synchronizedTest.new ReadWriteThread (false). Start ();}} reasons why shared variables are not visible between threads

1. Cross execution of threads

Eg: the execution steps of the above program are 1.1-"2.1 -" 2.2-"1.2

Eg: the execution steps of the above program are 1.2-"2.1 -" 2.2-"1.1

two。 Reordering combined with thread cross execution

Eg: 2.1and2.2After reordering

Int mid = number*3;if (ready) {result=mid;}

The updated value of the shared variable is not updated in time between the working memory and the main memory

Security code public class SynchronizedTest {/ * shared variable * / private boolean ready = false; private int result = 0; private int number = 1; / * write operation * / public synchronized void write () {ready = true;//1.1 number = 2 / / 1.2} / * read operation * / public synchronized void read () {if (ready) {/ / 2.1 result=number*3;//2.2} System.out.println ("value of result is:" + result) } / * * Internal thread class * / private class ReadWriteThread extends Thread {/ / determines whether the thread performs a read operation or a write operation private boolean flag; public ReadWriteThread (boolean flag) {this.flag=flag based on the flag parameter passed in the constructor } @ Override public void run () {if (flag) {/ / pass true in the constructor to perform the write operation write ();} else {/ / pass in false in the constructor to perform the read operation read () }} public static void main (String [] args) {SynchronizedTest synchronizedTest = new SynchronizedTest (); / / start the thread to perform the write operation synchronizedTest.new ReadWriteThread (true). Start (); / / start the thread to perform the read operation synchronizedTest.new ReadWriteThread (false). Start ();}}

These are all the contents of the article "how to achieve visibility in java multithreaded Synchronized". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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