Thread has order and visibility

Source: Internet
Author: User

There is no direct data interaction between multiple threads, and the interaction between them can only be achieved through shared variables.

An object of a class is shared among multiple threads, and this object is created in main memory ( heap Memory ) , each of which has its own working memory ( line Stacks ), the working memory stores a copy of the main memory object, and when the thread is manipulating the object, it first copies the object from the main memory to the working memory, then executes the code, changes the value, and finally flushes the main memory with the working memory.

When an object has replicas in multiple memory, if one memory modifies the shared variable, other threads should be able to see the modified value . this is visibility .

When multiple threads are executing, theCPU Schedules The threads randomly, and we do not know which step the current program is executing to switch to the next thread. This is ordered to ensure that the thread is executed in an orderly manner .

a method or block of code that uses synchronized adornments can be seen as an atomic operation .

each Lock object ( called Monitor in JLS) There are two queues, one is a ready queue, one is a blocking queue, the ready queue stores the thread that will acquire the lock, the blocking queue stores the blocked thread, and when a thread is awakened (notify) before it gets into the ready queue, waiting for CPU the scheduling, conversely, when a thread is wait , it enters the blocking queue and waits for the next one to be woken up. Looking at our example, when the first thread executes the output method, it obtains a synchronous lock, executes the output method, exactly at this point the second thread also executes the output method, but discovers that the synchronization lock is not released, the second thread enters the ready queue, waits for the lock to be released. One thread executes the mutex code procedure as follows:

1. obtain the synchronous lock;

2. clear the working memory;

3. Copy the object from the main memory to the working memory;

4. Execute the Code ( calculation or output, etc. );

5. refresh the main memory data;

6. Release the sync lock.

So, synchronized It guarantees the concurrency and order of multithreading, and ensures the memory visibility of multithreading.

Volatile is the second mechanism for multithreading in Java , where a variable can be modified by volatile , in which case the memory model ( main memory and thread working memory Ensure that all threads can see a consistent variable value.

Thread has order and visibility

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.