Learn Java Multi-Threading 4-memory visibility with examples

Source: Internet
Author: User

Top three we have mainly said that multi-threaded access to share mutable state needs to do the correct synchronization, to ensure that only one thread at a time to access the same data, we use the Synchronized keyword to achieve atomic operations.

Today we are here to meet another important aspect of synchronization: memory visibility, a concept that is well understood, is to ensure that at the same time, a shared mutable state presents itself with the latest state changes to the thread that accesses it.

We often encounter scenarios like this, a global variable counter, a thread responsible for updating the value, and some other threads to get it, then the visibility is the thread that gets the value, and can get the latest value to update the thread update.

Let's take a look at an example of what happens without synchronizing.

Package com.home.thread;/** * @author Gaoxu * */public class ThreadStart {static Boolean ready; static int number; Priv  Ate static class Readerthread extends thread{public void run () {number = 1;   }} private static class ReaderThread1 extends thread{public void run () {if (ready) {System.out.println ("=" +number);}  }} public static void main (string[] para) {new readerthread (). Start ();  Ready = true; New ReaderThread1 (). Start (); }}

The result of running the above code may be as follows:

The first type:

=0

The second Kind

=1

Only the second of these is the result we want to see.

We call the first situation phenomenon "reordering", the reordering phenomenon often produces invalid data, the first row is an invalid data, to solve this phenomenon must adopt a synchronous locking mechanism. (There are, of course, volatile variables, which we'll focus on later on.)

Let's take a look at how to implement a thread-safe variable integer class.

Package com.home.thread;/** * @author gaoxu * */  
@safepublic class Safethread {static int number; @safepublic  static synchronized int getnumber () {return number;} @safepublic  static synchronized void Setnumber (int number) {safethread.number = number;}}
Package com.home.thread;/** * @author Gaoxu *  */public class ThreadStart {private static class Readerthread extends T hread{public   Void Run () {   safethread.setnumber (1)}  } private static class ReaderThread1 extends thread{< c6/>public void Run () {     System.out.println ("=" +safethread.getnumber ());}  }  public static void main (string[] para) {    new Readerthread (). Start ();  New ReaderThread1 (). Start (); }}


After locking we will see that the output is always 1.

Let's summarize the meaning of lock synchronization: The meaning of the lock is not confined to the atomic operation behavior, but also can guarantee the memory visibility. Of course the most important point is that multiple read-write threads must be variable state variables that are guaranteed to be synchronized in the same lock.

We now know the main features, mutexes, and visibility of synchronization. But we know that the impact of synchronization on performance is very large, so how do we balance it?



Learn Java Multi-Threading 4-memory visibility with examples

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.