In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article shares with you the content of a sample analysis of concurrent access by Java threads. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Class ConcurrentThread {/ * analyze thread concurrent access code explanation reason * volatile keyword: * 1): ensures the visibility of different threads when operating on this variable, that is, a thread modifies the value of a variable This new value is immediately visible to other threads * 2): instruction reordering is disabled * volatile essentially tells JVM that the value of the current variable in the register (working memory) is uncertain and needs to be read from main memory * / private volatile int count = 0 Public void inc () {try {Thread.sleep (3);} catch (InterruptedException e) {e.printStackTrace ();} count++;} @ Override public String toString () {return "[count=" + count+ "]" }} /-- public class VolatileTest {public static void main (String [] args) {final ConcurrentThread counter = new ConcurrentThread (); for (int I = 0; I
< 1000; i++) { new Thread(new Runnable() { @Override public void run() { counter.inc(); } }).start(); } System.out.println(counter); }}Explanation: in Java's memory model, each thread runtime has a thread stack, and the thread stack holds the thread runtime variable value information.
When it is worthwhile for a thread to access an object:
1. Find the value of the variable in the heap memory through the reference of the object
2, load the specific value of the heap memory variable to the thread local memory, and establish a copy of the variable, then the thread no longer has any relationship with the value of the object in the heap memory variable, but directly modifies the value of the copy variable, and automatically writes back the value of the thread variable copy to the object in the heap variable at a certain time after modification (before the thread exits). This changes the value of the objects in the heap.
Combined with the above example, that is to say, there are 1000 child threads opened in the above main function, each thread has a copy of the variable, and each thread modifies the variable temporarily. When the thread ends, the modified value is written into the main memory, so there is a thread safety problem (getting the outdated variable value in the main memory), so the result can not be equal to 1000, usually less than 1000.
Thank you for reading! This is the end of this article on "sample Analysis of Java Thread concurrent access". I hope the above content can be of some help to you, so that you can learn more knowledge. If you think the article is good, you can share it out for more people to see!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.